Results 1 to 4 of 4

Thread: Connection problem with menubar using Designer

  1. #1
    Join Date
    Jan 2007
    Posts
    8
    Thanks
    4
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Question Connection problem with menubar using Designer

    Hi,

    This seems so simple but I haven't been able to figure it out yet.. All help will be appreciated :)

    I have tried to create a Mainwindow (mainwidget) with menubar holding an entry "Help", and I have created an "aboutAction"-item and dragged it into the help menu using the designer! Saved as main.ui! (check attachment)

    I have three other files: mainwindow.cpp, mainwindow.h and main.cpp (see below) and it seems to compile as it should but when I try to execute the About action nothing happens.
    I have tested the about() function by just executing it on program start and it shows a QMessageBox as intended, Now why doesn't the line:

    Qt Code:
    1. connect( aboutAction, SIGNAL(triggered()), this, SLOT(about()) );
    To copy to clipboard, switch view to plain text mode 

    ...seem to connect the menu-entry with the function

    Thanks in advance :

    Nikau


    mainwindow.cpp

    Qt Code:
    1. #include <QApplication>
    2. #include <QStatusBar>
    3. #include <QMessageBox>
    4. #include "mainwindow.h"
    5.  
    6. Mainwindow::Mainwindow( QWidget *parent ) : QMainWindow( parent )
    7. {
    8.  
    9. ui.setupUi( this );
    10. createActions();
    11. statusBar()->showMessage( tr("Done") );
    12.  
    13. }
    14.  
    15. void Mainwindow::about()//works
    16. {
    17. QMessageBox::about( this, tr("About Mainwindow"),
    18. tr("<h2>Mainwindow v. 0.01</h2>\n"
    19. "An application to help test various aspects of QT4\n"
    20. "Thanks to anybody helping out :-)\n") );
    21. }
    22.  
    23. void Mainwindow::createActions()
    24. {
    25. aboutAction = new QAction(tr("&About"), this);
    26. connect( aboutAction, SIGNAL(triggered()), this, SLOT(about()) );
    27. }
    To copy to clipboard, switch view to plain text mode 

    mainwindow.h
    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QMainWindow>
    5. #include "ui_Main.h"
    6.  
    7. class QAction;
    8. class QTextEdit;
    9. class QMenu;
    10.  
    11. class Mainwindow : public QMainWindow
    12. {
    13. Q_OBJECT
    14.  
    15. public:
    16. Mainwindow( QWidget *parent = 0 );
    17.  
    18. protected:
    19.  
    20. private slots:
    21. void createActions();
    22. void about();
    23.  
    24.  
    25. private:
    26.  
    27. QAction *aboutAction;
    28. Ui::MainWindow ui;
    29. };
    30.  
    31. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 

    main.cpp
    Qt Code:
    1. #include <QApplication>
    2.  
    3. #include "Mainwindow.h"
    4.  
    5. int main( int argc, char **argv )
    6. {
    7. QApplication app( argc, argv );
    8.  
    9. (new Mainwindow)->show();
    10.  
    11. return app.exec();
    12. }
    To copy to clipboard, switch view to plain text mode 
    Attached Files Attached Files

  2. #2
    Join Date
    Oct 2007
    Location
    Grenoble, France
    Posts
    80
    Thanked 9 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Connection problem with menubar using Designer

    You've declared aboutAction inside your UI with QtDesigner, so you should refere to it like this:
    Qt Code:
    1. connect( &ui.aboutAction, SIGNAL(triggered()), this, SLOT(about()) );
    To copy to clipboard, switch view to plain text mode 

    If you run your code from console I guess you'll get message that object aboutAction doesn't exist.
    Last edited by calhal; 30th March 2009 at 14:31. Reason: updated contents
    You have to run a level 3 diagnostic.

    Ashes to ashes, Qt to Qt ( wysota )

  3. The following user says thank you to calhal for this useful post:

    nikau (30th March 2009)

  4. #3
    Join Date
    Jan 2007
    Posts
    8
    Thanks
    4
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Thumbs up Re: Connection problem with menubar using Designer

    Quote Originally Posted by calhal View Post
    You've declared aboutAction inside your UI with QtDesigner, so you should refere to it like this:
    Qt Code:
    1. connect( &ui.aboutAction, SIGNAL(triggered()), this, SLOT(about()) );
    To copy to clipboard, switch view to plain text mode 

    If you run your code from console I guess you'll get message that object aboutAction doesn't exist.
    Actually what seemed to work is the following

    Qt Code:
    1. connect( ui.aboutAction, SIGNAL(triggered()), this, SLOT(about()) );
    To copy to clipboard, switch view to plain text mode 

    When would I use the &??

    I don't recal having to call ui.action when doing this...

    Well thank you for helping out :-)

  5. #4
    Join Date
    Oct 2007
    Location
    Grenoble, France
    Posts
    80
    Thanked 9 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Connection problem with menubar using Designer

    Ups, I was writing fast from my memory

    I think the answer is in QtAssistant:
    Qt Code:
    1. bool QObject::connect ( const QObject * sender, const char * signal,
    2. const QObject * receiver, const char * method,
    3. Qt::ConnectionType type = Qt::AutoConnection ) [static]
    To copy to clipboard, switch view to plain text mode 
    You have to run a level 3 diagnostic.

    Ashes to ashes, Qt to Qt ( wysota )

Similar Threads

  1. Signal-slots connection problem.
    By impeteperry in forum Qt Tools
    Replies: 26
    Last Post: 23rd January 2007, 14:33
  2. Basic: problem building designer application
    By kingslee in forum Qt Tools
    Replies: 2
    Last Post: 31st August 2006, 15:26
  3. connection problem
    By mhoover in forum Qt Programming
    Replies: 1
    Last Post: 7th July 2006, 22:14
  4. problem with designer
    By bashamehboob in forum Qt Tools
    Replies: 2
    Last Post: 12th April 2006, 07:46
  5. Facing problem with qt-4.1 designer
    By jnana in forum Qt Tools
    Replies: 4
    Last Post: 8th March 2006, 18:16

Tags for this Thread

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.