0

I'm building an application that requires a Qdialog, and a few buttons.

I am attempting to use the Command behavioural design pattern in my implementation. My project so far has 4 classes. (Please excuse the rough UML)

Command
+execute()

zoomInAndOut : Command
+execute()

MenuItem
-QPushButton
-command
+clicked()

Dialog

So within the dialog class, I create a menuItem (which has a QPushButton and Command member variable), and what I need to happen is that when the menuItems button has been clicked, it calls the menuItems "clicked" method (which in turn calls the commands execute method). I know that the "connect" function must be used, however after many, many attempts, I cannot get it to work correctly.

Within Dialog the code roughly looks like this

zoomInAndOut zoomCommand;
menuItem *zoom = new menuItem(new QPushButton("Zoom", this), QRect(QPoint(300, 0), QSize(100, 50)), &zoomCommand);
connect(zoom->getButton(), SIGNAL(clicked()), SLOT(zoom->clicked()));

As mentioned before the connect method is completely wrong, but you can see what I am attempting to achieve. How can I make this work?

Thank you in advance for any help.

Roy D. Porter
  • 159
  • 1
  • 11

2 Answers2

2

Change code

connect(zoom->getButton(), SIGNAL(clicked()), SLOT(zoom->clicked()));

to

connect(zoom->getButton(), SIGNAL(clicked()), zoom, SLOT(clicked()));

Shtol Krakov
  • 1,260
  • 9
  • 20
  • Hi! When attempting this it results in the compilation error stating: "No known conversion from 'menuItem * ' to 'const QObject *' for 3rd argument. – Roy D. Porter May 20 '16 at 05:59
  • Ok. What you returning on `getButton()` method? – Shtol Krakov May 20 '16 at 06:02
  • Is zoom's method `clicked()` in `public slots:` section of zoom's class? – Shtol Krakov May 20 '16 at 06:04
  • Yes, it is. I added both yours and Aarons solution. Which has now led me to this: http://imgur.com/WPVHLRY – Roy D. Porter May 20 '16 at 06:07
  • Yes, I believe I do - however the error shown was actually irrelevant and after a clean, and fresh build the QT is now telling me that I have a forward declaration of my QPushButton... You can see the code and error more cleanly here: http://imgur.com/u1v3lV4 – Roy D. Porter May 20 '16 at 06:14
  • 1
    Well, add `#include ` to class – Shtol Krakov May 20 '16 at 06:15
  • It compiled! Thank you! I have been working on this error for the past 2-3 hours. Thank you kindly. When I click the button, the program crashes, but as long as it compiles, I should be able to take it from here. :) – Roy D. Porter May 20 '16 at 06:22
1

Make sure your menuItem class contains the Q_OBJECT macro the line after the opening {. And make sure the clicked() method is in the slots section of the class body.

Aaron
  • 1,181
  • 7
  • 15
  • Hi! I have never heard of doing this, but I was able to add it after a quick google search. Which has led to the following screenshot: http://imgur.com/WPVHLRY I'm pretty confident I added the macro correctly, however when attempting to compile I receive the compiler linker error you can see at the bottom of the screenshot. – Roy D. Porter May 20 '16 at 06:06
  • 1
    Does it compile if you remove the macro ? (But you need it for SIGNAL and SLOTS to work within menuItem) – Aaron May 20 '16 at 06:18
  • 1
    You should put your class declaration in the header file. Is it? O_OBJECT won't work in a cpp file. See also http://stackoverflow.com/questions/4774291/q-object-throwing-undefined-reference-to-vtable-error – Aaron May 20 '16 at 06:21
  • Hi Aaron, a combination of yours and @someoneinthebox solution has got the program to compile. Thank you for your help! – Roy D. Porter May 20 '16 at 06:23