QPushButton

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

  • surrealsurreal Posts: 168

    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()));

     

  • surrealsurreal Posts: 168

    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.

  • Widdershins StudioWiddershins Studio Posts: 539
    edited November 2015

    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.

    Post edited by Widdershins Studio on
  • Widdershins StudioWiddershins Studio Posts: 539
    edited November 2015

    I'll drop my code in incase anyone has a bright idea.

    myfirstplugin.cpp

     

    #include <QtGui/QPushButton>#include <QtGui/QVBoxLayout>#include "dzapp.h"#include "dzbone.h"#include "dzfacetmesh.h"#include "dzhelpmgr.h"#include "dzobject.h"#include "dzscene.h"#include "dzshape.h"#include "dzskeleton.h"#include "dzstyle.h"#include "dzplugin.h"myFirstPlugin::myFirstPlugin() : DzPane("Hello there") {    QGroupBox *groupBox = new QGroupBox(tr("Exclusive Radio Buttons"));    QRadioButton *radio1 = new QRadioButton(tr("&Radio button 1"));    QRadioButton *radio2 = new QRadioButton(tr("R&adio button 2"));    QRadioButton *radio3 = new QRadioButton(tr("Ra&dio button 3"));    radio1->setChecked(true);    QGridLayout *gLay = new QGridLayout(groupBox);    gLay->addWidget(radio1);    gLay->addWidget(radio2);    gLay->addWidget(radio3);    //Create QPushButton    QPushButton *button1 = new QPushButton(tr("Button 1"));    button1->setObjectName("button1");    // Define the layout for the pane    QVBoxLayout *mainLyt = new QVBoxLayout();    mainLyt->addWidget(groupBox);    mainLyt->addWidget(button1);    mainLyt->addStretch(1);    // Set the layout for the pane    setLayout(mainLyt);    showPane();    connect(button1, SIGNAL(pressed()), this, SLOT(doThis()));}myFirstPlugin::~myFirstPlugin(){}void myFirstPlugin::doThis() {    QMessageBox* box = new QMessageBox();    box->setWindowTitle("Hello");    box->setText("You clicked !");    box->show();}

     

     

    myfirstplugin.h

     

     

    #ifndef MYFIRSTPLUGIN_H#define MYFIRSTPLUGIN_H#include "dzpane.h"#include "dzaction.h"#include <QtGui/QPushButton>class myFirstPluginAction : public DzPaneAction {    Q_OBJECTpublic:    myFirstPluginAction() : DzPaneAction("myFirstPlugin") {    }};class myFirstPlugin : public DzPane {    Q_OBJECTpublic:    myFirstPlugin();    ~myFirstPlugin();public slots:    void            doThis();private:    QPushButton     *button1;};#endif // MYFIRSTPLUGIN_H

     

    Post edited by Widdershins Studio on
  • Widdershins StudioWiddershins Studio Posts: 539
    edited November 2015

    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 laugh

    PHP was so much easierwink

    Post edited by Widdershins Studio on
  • surrealsurreal Posts: 168

    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 blush   

     

     

  • 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... laugh

  • 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.

Sign In or Register to comment.