Results 1 to 7 of 7

Thread: Compiler error when connecting a QAction of a menu bar

  1. #1
    Join Date
    Jun 2012
    Location
    Spain
    Posts
    16
    Qt products
    Qt4
    Platforms
    Windows

    Wink Compiler error when connecting a QAction of a menu bar

    Hi everyone,

    I'm migrating an old (well, really ancient) Windows application from Borland Builder 3 to Qt. I've started creating a new project, particularly a Qt Gui application. I've used as an example the editabletreemodel project included in the demos. Hence, the project includes simply a form (that I've renamed as VFIT_W) and a main file. Well, first of all I've created a menu bar (with no operative options; just to see the menu options) and (reading help everywhere) a status bar. While I designed this useless form, I've been able to compile and run the application.

    The problem has appeared when I've tried to activate the first option of the menu bar: Exit. I've written a simple function called ExitClick, and connected this function to Exit QAction. However, a compiler error appears. Can anyone tell me where's the mistake, please?

    The form code looks like this. This are the elements in the main window (basically the menu bar, as the status bar has been generated in the constructor):

    Form.png

    This is the header of the main window (VFIT_W.h file):

    Qt Code:
    1. #ifndef VFIT_W_H
    2. #define VFIT_W_H
    3.  
    4. #include <QMainWindow>
    5. #include <QLabel>
    6.  
    7. namespace Ui {
    8. class VFITW;
    9. }
    10.  
    11. class VFITW : public QMainWindow
    12. {
    13. Q_OBJECT
    14.  
    15. public:
    16. explicit VFITW(QWidget *parent = 0);
    17. ~VFITW();
    18.  
    19. private:
    20. Ui::VFITW *ui;
    21. QLabel *st_1, *st_2, *st_3;
    22.  
    23. private slots:
    24. void ExitClick();
    25. };
    26.  
    27. #endif // VFIT_W_H
    To copy to clipboard, switch view to plain text mode 

    This is the code of the main window (VFIT_W.cpp file):

    Qt Code:
    1. #include <QtGui>
    2. #include <QWidget>
    3. #include <QAction>
    4.  
    5. #include "VFIT_W.h"
    6. #include "ui_VFIT_W.h"
    7.  
    8. VFITW::VFITW(QWidget *parent) :
    9. QMainWindow(parent),
    10. ui(new Ui::VFITW)
    11. {
    12. ui->setupUi(this);
    13.  
    14. // Show main window maximized
    15. this->setWindowState(Qt::WindowMaximized);
    16.  
    17. // Create status bar
    18. st_1 = new QLabel("Left", this);
    19. st_1->setMinimumSize(150, 20);
    20. st_1->setMaximumSize(500, 20);
    21. st_1->setFrameStyle(QFrame::Panel | QFrame::Sunken);
    22. st_1->setAlignment(Qt::AlignCenter | Qt::AlignRight);
    23. st_1->setWhatsThis("Info left");
    24. st_2 = new QLabel("Middle", this);
    25. st_2->setMinimumSize(450, 20);
    26. st_2->setFrameStyle(QFrame::Panel | QFrame::Sunken);
    27. st_2->setAlignment(Qt::AlignCenter | Qt::AlignRight);
    28. st_2->setWhatsThis("Info middle");
    29. st_3 = new QLabel("Right", this);
    30. st_3->setMinimumSize(700, 20);
    31. st_3->setFrameStyle(QFrame::Panel | QFrame::Sunken);
    32. st_3->setAlignment(Qt::AlignCenter | Qt::AlignRight);
    33. st_3->setWhatsThis("Info right");
    34.  
    35. statusBar()->addPermanentWidget(st_1, 0);
    36. statusBar()->addPermanentWidget(st_2, 0);
    37. statusBar()->addPermanentWidget(st_3, 100);
    38.  
    39. connect(Exit, SIGNAL(triggered()), qApp, SLOT(ExitClick()));
    40.  
    41. }
    42.  
    43. VFITW::~VFITW()
    44. {
    45. delete ui;
    46. }
    47.  
    48. void VFITW::ExitClick()
    49. {
    50. exit(EXIT_SUCCESS);
    51. }
    To copy to clipboard, switch view to plain text mode 

    The error (error: 'Exit' was not declared in this scope) refers to the "connect" code line. In the editabletreemodel project that I've used as a model, the connection of the "exit" option of the main menu is made exactly the same (although the names of both the QAction and the function are different).

    On the other hand, I've kept the code related to the creation of the status bar because I also have a small problem with the "WhatsThis" messages in the status bar: they don't appear. I haven't seen anywhere if I need to activate or configure this property so I don't understand why they don't become visible.

    Finally, this is the code of the main.cpp file:

    Qt Code:
    1. #include <QtGui/QApplication>
    2. #include "VFIT_W.h"
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication a(argc, argv);
    7. VFITW w;
    8. w.showMaximized();
    9.  
    10. return a.exec();
    11. }
    To copy to clipboard, switch view to plain text mode 

    There are many differences from Builder 3, and I don't understand much of the automatic code generated (like "#include "ui_VFIT_W.h" " in the VFITW.cpp file...), but I hope to end up to manage. I wish there was an application which would "translate" code from Builder 3 to Qt! . Meanwhile, I'll have to do my best on my own or with some help...

    Thanks a lot!

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,013 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Compiler error when connecting a QAction of a menu bar

    Should be ui->Exit, most probably, since everything you did in Designer is part of the ui object.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Jun 2012
    Location
    Spain
    Posts
    16
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Compiler error when connecting a QAction of a menu bar

    Thank you so much! It worked, although I've also had to change "qApp" to "this" in the connect sentence (connect(ui->Exit, SIGNAL(triggered()), qApp, SLOT(ExitClick()));). What I don't understand is why I have to use ui-> to refer to "Exit" QAction, but not to refer to "statusBar" status bar. By the way, do you know why the hints (the "WhatsThis" messages) in the status bar don't appear?

    Thanks again.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,013 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Compiler error when connecting a QAction of a menu bar

    Quote Originally Posted by jcbaraza View Post
    What I don't understand is why I have to use ui-> to refer to "Exit" QAction, but not to refer to "statusBar" status bar.
    That's because ui->Exit is a member of the UI::Something class and statusBar() is really QMainWindow::statusBar() which is a method returning an object pointer in the QMainWindow class.

    I suggest you get a good book on object oriented programing and read it. Then things like that will become clear.

    By the way, do you know why the hints (the "WhatsThis" messages) in the status bar don't appear?
    They are not meant to appear there.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Jun 2012
    Location
    Spain
    Posts
    16
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Compiler error when connecting a QAction of a menu bar

    Thank you so much again! The problem lies on the big differences with Builder. There, both the main menu and the the status bar are children objects from the MainForm class. So this new philosophy put me offside.

    Regarding my trouble with trouble with hints, after reviewing my original code in Builder (made twelve years ago), I've realized that it wasn't as easy as I remembered. In that code, I needed to know which pannel (the equivalent in Builder to Qt's widgets) was under the mouse, and then modify the "Hint" property (equivalent to Qt's "toolTip") and then show it. So I think I'll have to do just the same: to use the mouse or status bar events to guess what (i.e., which widget) is under the mouse, and then to proceed.

    Another possibility that I've thought is to add another object different from QLabels as widgets in the status bar (such as QWidgets containing the QLabels), and associate the desired toolTip to each QWidget. Please remember my original code on lines 17 to 37 of VFITW.cpp code, included in my first post. The resulting status bar is like tis:

    Status_bar.jpg

    The final result should be that when moving the mouse on the status bar, three different messages should appear depending on the particular status bar "field" overflied, just like this:

    Status_bar_info_left.png Status_bar_info_middle.png Status_bar_info_right.png

    Thanks again. I'll tell how I managed with both options, or with any other that I could try .

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

    Default Re: Compiler error when connecting a QAction of a menu bar

    Just set the tool tip property of each widget (QWidget::setToolTip()) you add to the status bar and Qt will handle them for you.

  7. #7
    Join Date
    Jun 2012
    Location
    Spain
    Posts
    16
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Compiler error when connecting a QAction of a menu bar

    Great! It worked, and it was much easier than I thought. I only needed to type correctly "setToolTip" (instead of "setWhatsThis" in my first try) in the declaration of the hints:

    st_1->setToolTip("Info left");
    ...
    st_2->setToolTip("Info middle");
    ...
    st_3->setToolTip("Info right");
    Thanks!

Similar Threads

  1. Replies: 2
    Last Post: 29th August 2012, 18:57
  2. Connecting menu click with action
    By thefatladysingsopera in forum Newbie
    Replies: 1
    Last Post: 23rd August 2011, 14:52
  3. no compile error, but error on run with QMenu QAction
    By sincnarf in forum Qt Programming
    Replies: 4
    Last Post: 4th May 2011, 12:05
  4. fatal error C1001: An internal error has occurred in the compiler
    By noodles in forum Installation and Deployment
    Replies: 0
    Last Post: 12th August 2010, 12:24
  5. Context menu for QAction
    By drhex in forum Qt Programming
    Replies: 4
    Last Post: 29th September 2009, 19:01

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.