PDA

View Full Version : Qt Designer & uic. How to do custom stuff when a button is pushed (c++)



lazy
13th March 2020, 23:27
Hello I've started to use Qt Designer 5.14

In the Designer, I created a simple main window with one button. I run uic.exe to generate a C++ header for it.

To show the window I do:

QMainWindow w;
Ui::MainWindow mw; //The uic generated mainwindow class
mw.setupUi(&w);
w.show();

So far so good.

Now comes the part I don't understand
I want the button to generate a signal that I can handle in C++ code.

So in Designer I added a new slot to the mainwindow called do_something()
And then in the Signal/Slot editor added Sender: pushButton Signal: clicked() Receiver: mainwindow Slot: do_something()

This, I thought, would generate a do_something() method in the generated Ui::MainWindow class header by uic, so I could then implement the method and add what should happen when the button is pushed.

But the only thing that gets generated is:
QObject::connect(pushButton, SIGNAL(clicked()), MainWindow, SLOT(do_something()));

I do not understand where I should implement do_something() so I can make stuff happen when the button is pushed.

I may have missunderstood how Qt Works... :(

Thanks for any help!

lazy
14th March 2020, 12:27
So I think I got a bit closer when I realised I should create a new class inhereting from QMainWindow and there implement the slots.

I inherit from QMainWindow and added the Q_OBJECT macro to my new class.
But then I get unresolved external symbols error when linking. Some virtual methods declared by the Q_OBJECT macro are missing.
Am I supposed to implement them myself or is there some other macro to actually get the implementation of them?

I think I actually got it now :)
I create a new header file for my window class, then run moc.exe my_window.h -o moc_my_window.cpp
That generates all the stuff I need. I can now implement my code when I push the button :)

Thanks myself for all the help! ;)

d_stranz
14th March 2020, 14:54
Sounds like you are on the right track. Deriving from a QWidget- (for GUI things) or QObject-based class (for non-GUI things) is almost always required when you want something to happen in response to a signal. A QDialog by itself is pretty useless - you can define the UI for it, but you can't connect to any of the signals from the QWidgets inside it unless you derive your own class from QDialog and create the slots and connect them in there. (There are other ways, but it involves ugly and discouraged things like exposing details of the UI objects to the outside world. So don't do that - declare your GUI member variables private or protected.).

There are some things that work right out of the box, because they are designed to provide useful shortcuts. For example, you can create a QSqlQueryModel or a QStandardItemModel and simply install them as the source model on a QTableView and everything just works. This is because internally, these classes have all of the plumbing needed to communicate with each other.

But for the most part, you will need to follow the same process you taught yourself. Have fun.