Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Need a little help with QPushButton & connect

Need a little help with QPushButton & connect

Scheduled Pinned Locked Moved Solved General and Desktop
qpushbuttonconnect
35 Posts 5 Posters 12.3k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mrmorph
    wrote on 18 Nov 2015, 22:16 last edited by mrmorph
    #1

    I have just started working on a plugin for the program Daz Studio and I'm stuck on what I thought would be a simple process to get an event when a button is pressed. Alas that it does not work.

    So I have in my main .cpp :

    QPushButton* button1 = new QPushButton(tr("Button 1"));
    mainLyt->addWidget(button1);
    connect(button1, SIGNAL(clicked()), this, SLOT (doSomething()));
    

    Then a function :

    void DzSceneInfoPane::doSomething()
    {
    	QMessageBox* box = new QMessageBox();
    	box->setWindowTitle("Hello");
    	box->setText("You clicked !");
    	box->show();		
    }
    

    And in my header files :

    public slots:
    	void			doSomething();
    

    Apologies if my terminology is off, I'm new to C++.

    Could anyone help ?

    Also there does not seem to be a way to format code in this message.

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 18 Nov 2015, 22:25 last edited by
      #2

      Hi and welcome to devnet,

      For Signals and Slots to work you need an event loop running, usually through the call of QApplication's exec function. Do you have that ?

      Yes there is, the forum uses Markdown, you can surround your code with three back ticks (three back ticks, new line(s) with code and again three back ticks after a new line after your last code line)

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • M Offline
        M Offline
        mrmorph
        wrote on 18 Nov 2015, 22:34 last edited by
        #3

        Hello there !

        Thanks for your reply.

        I edited my message, thanks for the tip.

        Currently I am just playing with some examples so I am inserting code to see what it does layout-wise. Then later I can start proper.

        I don't at this stage know how to get a loop running, but I will look into it.

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 18 Nov 2015, 22:40 last edited by
          #4

          The shortest way to get started is to create a new widget project and you'll have everything you need.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          1
          • M Offline
            M Offline
            mrmorph
            wrote on 19 Nov 2015, 02:03 last edited by
            #5

            Does my code above look right ?

            In this code I am playing with there are already SIGNALS and SLOTS being used.

            I'm just wondering if my code above is okay.

            After spending a few hours trying to get one button to work I'm losing the will to live ;)

            J 1 Reply Last reply 19 Nov 2015, 02:33
            0
            • M mrmorph
              19 Nov 2015, 02:03

              Does my code above look right ?

              In this code I am playing with there are already SIGNALS and SLOTS being used.

              I'm just wondering if my code above is okay.

              After spending a few hours trying to get one button to work I'm losing the will to live ;)

              J Offline
              J Offline
              JKSH
              Moderators
              wrote on 19 Nov 2015, 02:33 last edited by
              #6

              @mrmorph said:

              Does my code above look right ?

              In this code I am playing with there are already SIGNALS and SLOTS being used.

              I'm just wondering if my code above is okay.

              Unfortunately, you haven't posted enough code for us to tell if your code is right or not.

              Without seeing your code, my advice is:

              • Make sure your DzSceneInfoPane header contains the Q_OBJECT macro. If you don't, you can't define your own signals or slots.
              • Always pay attention to error and warning messages that you get. They contain valuable information about what's wrong.

              After spending a few hours trying to get one button to work I'm losing the will to live ;)

              I suggest following these tutorials. They are extremely simple, and use QPushButton:

              • https://doc.qt.io/archives/4.3/tutorial-t1.html -- This just makes a QPushButton that does nothing
              • https://doc.qt.io/archives/4.3/tutorial-t2.html -- This shows you how to connect a QPushButton to another class's slot

              At the botton of Tutorial 1, it says "If you have typed in the source code manually, you will need to follow these instructions"... ignore that part.

              Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

              1 Reply Last reply
              0
              • M Offline
                M Offline
                mrmorph
                wrote on 19 Nov 2015, 03:33 last edited by
                #7

                Yes thank you, I think you are both right. I will have to start from fresh and learn step by step.

                Thanks to you both for helping, I appreciate it :)

                1 Reply Last reply
                0
                • J Offline
                  J Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on 19 Nov 2015, 05:27 last edited by
                  #8

                  You have a memory leak in:

                  void DzSceneInfoPane::doSomething()
                  {
                      QMessageBox* box = new QMessageBox();
                      box->setWindowTitle("Hello");
                      box->setText("You clicked !");
                      box->show();        
                  }
                  

                  You never delete box.

                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  0
                  • M Offline
                    M Offline
                    mrmorph
                    wrote on 22 Nov 2015, 19:41 last edited by SGaist
                    #9

                    I started fresh and am getting there in other areas, but this darned button is still a problem.

                    In the examples you kindly posted there is this :

                    #include <QApplication>
                     #include <QPushButton>
                    
                     int main(int argc, char *argv[])
                     {
                         QApplication app(argc, argv);
                    
                         QPushButton hello("Hello world!");
                         hello.resize(100, 30);
                    
                         hello.show();
                         return app.exec();
                     }
                    

                    Which is fine, but the examples I am working from do not use main. Other wise I have set things up as suggested, but I get nada. Here's my code... edit the forum formatting does not seem to work today.

                    CPP :

                    #include "myfirstplugin.h"
                    
                    #include <QtCore/QObject>
                    #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");
                    
                        QMetaObject::connectSlotsByName(this);
                    
                    	// 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();
                    }
                    

                    And my header :

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

                    [edit: fixed coding tags, three back-ticks SGaist]

                    1 Reply Last reply
                    0
                    • mrjjM Offline
                      mrjjM Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on 22 Nov 2015, 21:01 last edited by
                      #10

                      hi
                      If you don't have a main then you most likely don't have

                      int main(int argc, char *argv[])
                      {
                      QApplication app(argc, argv);
                      return app.exec();
                      }

                      the app.exec() is an event loop and it makes signals and slot works.
                      So if its not running then no "click" will
                      ever work.

                      So it seem you are trying to make plugin for another program.
                      Does this program expect it to be a DLL ?
                      Are the samples you are using, for building a plugin with qt?

                      when there is no main, then no much will happen when you run it.

                      1 Reply Last reply
                      1
                      • M Offline
                        M Offline
                        mrmorph
                        wrote on 22 Nov 2015, 21:51 last edited by mrmorph
                        #11

                        No I don't have that.

                        But the samples I was working from are using SLOTS and SIGNALS without it.

                        It's a plugin yes - it's for Daz Studio, a graphics program.

                        It uses DLLs yes. I can successfully build the DLL and it loads and will show my widgets within the main program.

                        But whatever I try I can't get it to detect when I click on the button.

                        Maybe there is a library I am missing, but as far as I can tell I'm using all the same includes as the examples that have the working events.

                        mrjjM 1 Reply Last reply 22 Nov 2015, 21:57
                        0
                        • M mrmorph
                          22 Nov 2015, 21:51

                          No I don't have that.

                          But the samples I was working from are using SLOTS and SIGNALS without it.

                          It's a plugin yes - it's for Daz Studio, a graphics program.

                          It uses DLLs yes. I can successfully build the DLL and it loads and will show my widgets within the main program.

                          But whatever I try I can't get it to detect when I click on the button.

                          Maybe there is a library I am missing, but as far as I can tell I'm using all the same includes as the examples that have the working events.

                          mrjjM Offline
                          mrjjM Offline
                          mrjj
                          Lifetime Qt Champion
                          wrote on 22 Nov 2015, 21:57 last edited by
                          #12

                          @mrmorph
                          Ok so the examples are for Qt ?
                          And all loads in Daz Studio and you can see the button etc?
                          I assume this
                          connect(button1, SIGNAL(pressed()), this, SLOT(doThis()));
                          is the button in question ?
                          try output if connect is ok.
                          bool status = connect(button1, SIGNAL(pressed()), this, SLOT(doThis()));
                          if (!status)
                          QMessageBox...

                          to see if the actual connect that fails

                          Do you have a link for the examples ?
                          Must be something simple missing.

                          1 Reply Last reply
                          1
                          • M Offline
                            M Offline
                            mrmorph
                            wrote on 22 Nov 2015, 22:14 last edited by mrmorph
                            #13

                            Hi, thanks for trying to help.

                            And sorry yes, the samples are for Daz Studio yes and they work fine.

                            Everything loads yes and I can see the radio buttons and button - they are in a window... as in this snip...

                            Image to show it working in the main program...

                            button1 is the one I am trying to get to work yes.

                            I inserted this as suggested.

                            bool status = connect(button1, SIGNAL(pressed()), this, SLOT(doThis()));
                            
                            	if (status) {
                            		QMessageBox* box = new QMessageBox();
                            		box->setWindowTitle("Hello");
                            		box->setText("Works");
                            		box->show();
                            	}
                            	else {
                            		QMessageBox* box = new QMessageBox();
                            		box->setWindowTitle("Hello");
                            		box->setText("Does not work");
                            		box->show();
                            	}
                            

                            The connect appears to fail as I get 'Does not work'.

                            Thanks for formatting my previous code properly, I got the wrong key.

                            EDIT Opps, you wanted the samples. They are free here, but you would need to register. I don't think I can distribute the files myself.

                            http://www.daz3d.com/daz-studio-4-5-sdk

                            1 Reply Last reply
                            0
                            • mrjjM Offline
                              mrjjM Offline
                              mrjj
                              Lifetime Qt Champion
                              wrote on 22 Nov 2015, 22:20 last edited by mrjj
                              #14

                              @mrmorph said:
                              Hmm, one note

                              QPushButton *button1 = new QPushButton(tr("Button 1"));
                              you make new one but in .h you have one in class myFirstPlugin

                              so should it be
                              button1 = new QPushButton(tr("Button 1")); ?

                              also, since it fails, (the connect) we need to find out why

                              could you try the new syntax as it catch stuff compile time

                              connect(button1, &QPushButton::released, this, &myFirstPlugin::doThis);

                              1 Reply Last reply
                              1
                              • M Offline
                                M Offline
                                mrmorph
                                wrote on 22 Nov 2015, 22:29 last edited by
                                #15

                                Tried

                                button1 = new QPushButton(tr("Button 1"));

                                But still the same, does not work.

                                mrjjM 1 Reply Last reply 22 Nov 2015, 22:30
                                0
                                • M mrmorph
                                  22 Nov 2015, 22:29

                                  Tried

                                  button1 = new QPushButton(tr("Button 1"));

                                  But still the same, does not work.

                                  mrjjM Offline
                                  mrjjM Offline
                                  mrjj
                                  Lifetime Qt Champion
                                  wrote on 22 Nov 2015, 22:30 last edited by
                                  #16

                                  @mrmorph
                                  well was just a note. the real issues is why connect fails.
                                  can you try the new syntax
                                  connect(button1, &QPushButton::released, this, &myFirstPlugin::doThis);

                                  1 Reply Last reply
                                  1
                                  • M Offline
                                    M Offline
                                    mrmorph
                                    wrote on 22 Nov 2015, 22:37 last edited by
                                    #17

                                    Visual Studio does not like that.

                                    For connect there is a 'no instance of overload function "myFirstPlugin::connect" matches the argument list......'

                                    And &QPushButton::released is inaccessible...

                                    BTW, I am using Qt 4.8.1 as that's what Daz Studio is using.

                                    Sorry about the slow replies, it's not letting me post as I am a noob. Some 500 second limit.

                                    mrjjM 1 Reply Last reply 22 Nov 2015, 22:43
                                    0
                                    • M mrmorph
                                      22 Nov 2015, 22:37

                                      Visual Studio does not like that.

                                      For connect there is a 'no instance of overload function "myFirstPlugin::connect" matches the argument list......'

                                      And &QPushButton::released is inaccessible...

                                      BTW, I am using Qt 4.8.1 as that's what Daz Studio is using.

                                      Sorry about the slow replies, it's not letting me post as I am a noob. Some 500 second limit.

                                      mrjjM Offline
                                      mrjjM Offline
                                      mrjj
                                      Lifetime Qt Champion
                                      wrote on 22 Nov 2015, 22:43 last edited by
                                      #18

                                      @mrmorph
                                      oh. sorry. thought it was 5.0+
                                      forget new syntax then.

                                      since connect fails , there must be something wrong.
                                      but all seems fine.
                                      doThis is public slot. buttons1 is allocated.
                                      just so I 100% sure , can u try the released signal instead?
                                      connect(button1, SIGNAL(released()), this, SLOT(doThis()));
                                      and see if still say "not works"

                                      np. its spam protection but is indeed annoying sometimes.
                                      ps. QMessageBox* box = new QMessageBox();
                                      need a delete box; else its a leak but ok for now.

                                      1 Reply Last reply
                                      1
                                      • M Offline
                                        M Offline
                                        mrmorph
                                        wrote on 22 Nov 2015, 22:51 last edited by
                                        #19

                                        No problem, I should have mentioned I was on an older Qt.

                                        Same thing with released().

                                        Thanks for the tip about the MessagBox delete, someone else did mention it, but it was always just a temporary thing.

                                        I wonder if I have to hook into something else that's in Daz Studio.

                                        Just as an example, the sample I am working from as reference uses connect like this :

                                        connect( dzScene, SIGNAL(sceneLoadStarting()), this, SLOT(blockRefresh()) );
                                        

                                        Now it also has this include :

                                        #include "dzscene.h"
                                        

                                        The scene would be the main graphics window and other things. Perhaps there is some loop being started in there because it uses dzScene in the connect, not a widget or anything like I am using.

                                        Maybe I need to include another library to get the loop for the widgets.

                                        Just grabbing at straws.

                                        mrjjM 1 Reply Last reply 22 Nov 2015, 23:00
                                        0
                                        • M mrmorph
                                          22 Nov 2015, 22:51

                                          No problem, I should have mentioned I was on an older Qt.

                                          Same thing with released().

                                          Thanks for the tip about the MessagBox delete, someone else did mention it, but it was always just a temporary thing.

                                          I wonder if I have to hook into something else that's in Daz Studio.

                                          Just as an example, the sample I am working from as reference uses connect like this :

                                          connect( dzScene, SIGNAL(sceneLoadStarting()), this, SLOT(blockRefresh()) );
                                          

                                          Now it also has this include :

                                          #include "dzscene.h"
                                          

                                          The scene would be the main graphics window and other things. Perhaps there is some loop being started in there because it uses dzScene in the connect, not a widget or anything like I am using.

                                          Maybe I need to include another library to get the loop for the widgets.

                                          Just grabbing at straws.

                                          mrjjM Offline
                                          mrjjM Offline
                                          mrjj
                                          Lifetime Qt Champion
                                          wrote on 22 Nov 2015, 23:00 last edited by mrjj
                                          #20

                                          @mrmorph said:

                                          connect( dzScene, SIGNAL(sceneLoadStarting()), this, SLOT(blockRefresh()) );

                                          On the signal sceneLoadStarting call blockRefresh
                                          so that is very like button.
                                          Do you have access to a dzScene instance? Like the default scene ?
                                          you could try to hook it up with your doThis and then load new scene or what ever
                                          that signal means.

                                          Whole SDK seems to be build with Qt so might have global event loop for plugins.
                                          So I doubt you need anything else since it loads DLL.

                                          But I don't like that connect fails. as even no event loop, the connect should not fail
                                          as far as I know .
                                          But syntax seems fine. so I wonder.

                                          When you click on button, it does animate ? Meaning show being pressed ?

                                          Also the default examples do work? I assume they use some gui too?

                                          also, could you move connect over "showpane"
                                          I assume pane shows then it says "not work" while pane is shown?

                                          1 Reply Last reply
                                          1

                                          5/35

                                          19 Nov 2015, 02:03

                                          topic:navigator.unread, 30
                                          • Login

                                          • Login or register to search.
                                          5 out of 35
                                          • First post
                                            5/35
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • Users
                                          • Groups
                                          • Search
                                          • Get Qt Extensions
                                          • Unsolved