Results 1 to 12 of 12

Thread: signals/slot functions not automatically generated in the .cpp or .h file?

  1. #1
    Join Date
    Dec 2010
    Posts
    11
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default signals/slot functions not automatically generated in the .cpp or .h file?

    I have an application generated with QT wizard, using QMainwindow as the main class. In the .ui file, I created a menu with File, Open, etc. I’ve also created a pushbutton that clears the text of a Rich Text Edit control that’s on the form(and actually works!). I did this with the Signal and slot mechanism in QT designer, but I don’t see the functions for this in the code. I’m a bit confused as to what to do from here. Questions:

    1)Do signal and slot functions get automatically generated in the code or do they have to be manually added?

    2)How would I start to create additional dialog boxes to be attached to the menus? For example, if I want to invoke an open file dialog from File->Open, how would I do this?

    3)How can I add additional dialogs to my menus using QT designer?

    Please help, thx
    joe

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: signals/slot functions not automatically generated in the .cpp or .h file?

    Quote Originally Posted by joebats72 View Post
    1)Do signal and slot functions get automatically generated in the code or do they have to be manually added?
    You have connected a signal that already exists (clicked()) in the code of QPushButton to a slot that already exists in the code of QTextEdit (clear()). You don't have to create these routines. Designer has generated the code to connect the two pre-existing methods. You can see that code in a file ui_mainwindow.h (a name matching your source file) that is generated when you build your project.

    You have to declare any new signal that you wish to create for your own QObject derivatives, and let moc generate the code for that signal.
    You have to declare and implement any new slot that you wish to create for your own QObject derivatives.
    Open Qt Assistant and read about Signals and Slots
    2)How would I start to create additional dialog boxes to be attached to the menus? For example, if I want to invoke an open file dialog from File->Open, how would I do this?
    Open Qt Assistant and read the Standard Dialogs and the Application example. These examples cover everything you have asked, and there are plenty of others.
    3)How can I add additional dialogs to my menus using QT designer?
    Qt Designer designs a single UI element at a time. You design the new dialog using Designer and write code to create and display it from other places.

  3. #3
    Join Date
    Dec 2010
    Posts
    11
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: signals/slot functions not automatically generated in the .cpp or .h file?

    Hi, thanks for your input but I'm still having trouble here. I have a new dialog that I am trying to call from MainWindow. I included the "ui_dialog.h" in the MainWindow.cpp file. I have a menu with File, Test, Exit. I am trying to call the new dialog from "Test" but it just doesn't work. I am not sure how to display it. These functions are not found with the intellisense, but appear in the ui_dialog.h file. please help...I'm thinking something like this should work but doesnt:

    void MainWindow::actionTest()
    {
    Dialog dlg;
    dlg.show();

    }

  4. #4
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: signals/slot functions not automatically generated in the .cpp or .h file?

    Dialog is showed and immediatly closed because out of scope. Try something like this :
    Qt Code:
    1. void MainWindow::actionTest()
    2. {
    3. Dialog *dlg = new Dialog();
    4. //to auto delete after close
    5. dlg->setAttribute( Qt::WA_DeleteOnClose, true );
    6. dlg->show();
    7. }
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: signals/slot functions not automatically generated in the .cpp or .h file?

    Quote Originally Posted by joebats72 View Post
    Hi, thanks for your input but I'm still having trouble here. I have a new dialog that I am trying to call from MainWindow. I included the "ui_dialog.h" in the MainWindow.cpp file.
    There should be a class Dialog defined in a header (usually dialog.h) that you include in the MainWindow.cpp file. The machine generated ui_dialog.h file should remain private to the Dialog class implementation (usually dialog.cpp).

    I have a menu with File, Test, Exit. I am trying to call the new dialog from "Test" but it just doesn't work. I am not sure how to display it. These functions are not found with the intellisense, but appear in the ui_dialog.h file. please help...I'm thinking something like this should work but doesnt:

    void MainWindow::actionTest()
    {
    Dialog dlg;
    dlg.show();

    }
    As pointed out above by Lesiok, the dlg instance of Dialog goes out of scope immediately after you show() the non-modal dialog because show() return immediately. If the dialog is meant to be non-modal then you need to allocate dlg on the heap as Lesiok says. If the dialog was meant to be modal then you should use QDialog::exec() rather than QWidget::show().

  6. #6
    Join Date
    Dec 2010
    Posts
    11
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: signals/slot functions not automatically generated in the .cpp or .h file?

    Thanks again Chris & Losiok, but at this point I'm at a loss. I've attached the project, if someone can please check to see what the issue is. I've tried the options aforementioned and now the code builds and runs, but the dialog is not displayed when I select "Test" from the menu. Can you please help, thx joe
    Attached Files Attached Files

  7. #7
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: signals/slot functions not automatically generated in the .cpp or .h file?

    Where have you connected the menu action triggered() signal to the MainWindow::actionTest() slot you want to execute?

  8. #8
    Join Date
    Dec 2010
    Posts
    11
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: signals/slot functions not automatically generated in the .cpp or .h file?

    Hi, and thanks for your quick response. I actually did that now in QT designer, built the code but it still doesn't work. Not sure if I got this right, but here's a snapshot of what I did in Qt designer in my new version.
    Attached Files Attached Files

  9. #9
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: signals/slot functions not automatically generated in the .cpp or .h file?

    You connected the action's triggered() signal to the show() slot of the main window (with no visible effect since the main window is already visible if the user can activate the action). Why do expect that MainWindow::show() will run the code in the actionTest() slot?

    Try doing this connection in the MainWindow constructor without using Designer. You will probably learn more about what is going on.

    You can do this in Designer but you have to manually tell Designer about the custom slots for MainWindow so that they appear in the relevant slot editor drop-down.

  10. #10
    Join Date
    Dec 2010
    Posts
    11
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: signals/slot functions not automatically generated in the .cpp or .h file?

    Hi, thanks for pointing out my error. I am trying to connect the menu item (actionTest) to invoke my dialog. The “receiver” of the “signal” should be the dialog, and I think the slot should be “show()”. As you can see, I do not see the dialog object to “receive” the signal in Qt designer. Also, I tried to code this as you stated but it doesn’t work, so most likely I’m doing something wrong. What am I missing here? Can you provide a code snippet to assist here please? See code below and Qt designer snapshot attached.
    Thanks again,
    Joe



    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include "testdialog.h" //dialog header
    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);
    //TestDialog dlg;
    // dlg.show(); //this works, but the dialog is invoked before main app
    // dlg.exec();

    // connect(actionTest(), SIGNAL(triggered(), this, SLOT(show());
    //connect(MainWindow::show(),SIGNAL(triggered()),thi s, SLOT(show())));
    connect(actionTest(), SIGNAL(triggered()), TestDialog::show(), SLOT(show());
    }


    MainWindow::~MainWindow()
    {
    delete ui;
    }
    Attached Files Attached Files

  11. #11
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: signals/slot functions not automatically generated in the .cpp or .h file?

    No, the receiver of the signal should a be a slot that creates and shows the dialog: you had one called actionTest() in your main window class. The dialog object doesn't exist until you create it.

    Your connect() calls are broken and should be generating a compiler error or runtime warning. The arguments to the connect() are:
    • A pointer to the sender QObject
    • the SIGNAL() being sent
    • A pointer to the receiving object
    • the SLOT() receiving the signals when sent


    So, with the slot in your main window class renamed doStuff() to highlight the distinction from the actionTest QAction:
    Qt Code:
    1. connect(ui->actionTest, SIGNAL(triggered()), this, SLOT(doStuff()));
    To copy to clipboard, switch view to plain text mode 
    is closer to the mark.

  12. #12
    Join Date
    Dec 2010
    Posts
    11
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: signals/slot functions not automatically generated in the .cpp or .h file?

    It works! I think I have a better handle on this now that I see how it works. Thanks very much for your help.

    Joe

Similar Threads

  1. Replies: 3
    Last Post: 6th December 2010, 15:23
  2. Replies: 7
    Last Post: 25th November 2009, 13:10
  3. Replies: 0
    Last Post: 1st August 2009, 09:05
  4. Replies: 2
    Last Post: 20th December 2008, 06:17
  5. SLOT or functions
    By mickey in forum Newbie
    Replies: 1
    Last Post: 8th February 2006, 19:45

Bookmarks

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.