QPushButton
Widdershins Studio
Posts: 539
Can't figure this out having tried various examples and have been fiddling for hours.
From what I have seen this is what one needs :
QPushButton* button1 = new QPushButton(tr("Button 1"));mainLyt->addWidget(button1);connect(button1, SIGNAL(clicked()), this, SLOT (doSomething()));
Then a function to do something :
void DzSceneInfoPane::doSomething(){ QMessageBox* box = new QMessageBox(); box->setWindowTitle("Hello"); box->setText("You clicked !"); box->show(); }
I'm still just playing with the samples, hence the class name.
In the header file I added :
public slots: void doSomething();
But I get nothing.
Could someone please advise before I lose all my hair !
Cheers :)
Comments
The QPushButton's clicked signal requires a bool parameter.
void QAbstractButton::clicked(bool checked)
You have to mirror that in your connect statement, even if you are not going to use that parameter. You don't need to pass in or declare a variable for the parameter just make sure that it models the signal fully.
Thus your connect statement should be:
connect(button1, SIGNAL(clicked(bool)), this, SLOT (doSomething()));
Are you sure you that clicked(bool checked = false) is the right signal you want to listen for?
I would probably have used pressed() or released() instead if I wanted to listen for the user clicking on the button. But they won't fire if you are changing the button's clicked state programmatically.
Thanks, yes I tried pressed() - I tried everything I could lol
I've decided to start fresh and see how the whole process works. I'm getting there, a lot of things make more sense today.
I still can't get this to work with fresh code.
I asked on the Qt forum and they said SLOTS and SIGNAL need to be 'in the loop'.
I can understand this, but don't know how to implement it.
The Qt docs say the loop starts with exec(). But I'm not really sure where to put it.
Any thoughts on that ?
It's odd because the samples example Info Pane one uses slots but I don't see any exec() statement anywhere, I don't know how it's started the loop thing.
I'll drop my code in incase anyone has a bright idea.
myfirstplugin.cpp
myfirstplugin.h
I found a helpful person over on the Qt forums who helped.
We went through everything. Turns out I needed to rebuild the moc file to add the slots I had put in.
So there you go. If one day some other bedraggled person who has spent hours trying to get a button to say something when you click on it, thank the nice people at Qt
PHP was so much easier
Good to see you found the problem.
I set the custom build properties in my header files so that the moc files are rebuilt every time I build the solution.
In VS in the properties of the header file I set the CONFIGURATION PROPERTIES > GENERAL > ITEM TYPE to "Custom Build Tool", then hit APPLY. Then in CONFIGURATION PROPERTIES > CUSTOM BUILD TOOL > GENERAL
COMMAND LINE ..\..\..\bin\$(Platform)\moc "%(FullPath)" -o .\moc\%(Filename)_moc.cpp
DESCRIPTION Mocing "%(Filename)"...
OUTPUTS .\%(Filename)_moc.cpp
The "..\..\..\bin\$(Platform)\moc" is the relative path to the BIN\x64\MOC.EXE (or to BIN\Win32\MOC.EXE if biluding for 32bit).
PHP easy! for some
Thanks, I'll have a play with that - for now I was just getting QtCreator to build it.
I agree it would be easier just doing it in VS.
Well PHP wasn't easy, just compared to C++ so far...
Did you have to add anything for the signal built into the widget? Or just for the new slot?
I finally got the moc file straigtened out for the existing slots after I renamed everythin, but I still can't get a combobox widget standard signal to call a working slot routine.
So was wondering if you had to add anything to the moc tables for the standard signal. I thought those were built in.
No it's all just as layed out here.
Declare the button in the header and the routine it calls in the slots.
Then once you've created your widget then use your connect in the main cpp.
I've done nothing with the moc manually at all, it just gets built automatically.