Results 1 to 3 of 3

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

  1. #1
    Join Date
    Mar 2020
    Qt products

    Question Qt Designer & uic. How to do custom stuff when a button is pushed (c++)

    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

    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!

  2. #2
    Join Date
    Mar 2020
    Qt products

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

    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!
    Last edited by lazy; 14th March 2020 at 12:25.

  3. #3
    Join Date
    Jan 2008
    Alameda, CA, USA
    Thanked 803 Times in 792 Posts
    Qt products
    Windows Android

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

    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.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. Replies: 0
    Last Post: 27th June 2017, 23:24
  2. qt pushed icon and widgetbox
    By giugio in forum Qt Programming
    Replies: 0
    Last Post: 9th November 2012, 10:26
  3. Designer Plugin - multiple labels button
    By tihoulas in forum Qt Programming
    Replies: 2
    Last Post: 7th October 2010, 20:32
  4. Replies: 5
    Last Post: 30th July 2009, 12:37
  5. Custom widget: a button within a button
    By Gnurou in forum Qt Programming
    Replies: 7
    Last Post: 18th June 2009, 09:03

Tags for this Thread


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.