Results 1 to 7 of 7

Thread: General: Structure of a Qt mainwindow program

  1. #1
    Join Date
    Jul 2009
    Posts
    13
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default General: Structure of a Qt mainwindow program

    Hello everybody,

    I wonder how to layout the classes for a Qt program. I am using the designer for generating the *.ui and the ui_mainwindow.h.
    Main instantiates an mainwindow object from a MainWindow class, which calls the setupUi method of the ui_mainwindow.h in its constructor. So far it is standard, I guess.

    I thought, it is a good idea, to have several GUI related helper classes that can execute some special tasks, like plotting different types of graphs or adjust the controll widgets to the current user-operations... Is that recommended or should I do all that stuff in the MainWindow class?

    If I use these helper classes, they need to have access to the widget objects instantiated in the setupUi method. How can I do this?
    At the moment I am justing handing over a pointer of the widget to the helper class so I can manipulate that widget in the helper class.
    But I can imagine that this can become very tedious if the program is growing more complex.
    So, how can I, and this is the most important question to me, inherit the MainWindow or ui_MainWindow classes with the helper classes, so that the widgets instantiated in setupUi are accessible in the helper classes?

    I am happy about every hint

    greetz, kai

  2. #2
    Join Date
    Aug 2009
    Posts
    10
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: General: Structure of a Qt mainwindow program

    I think you mean to say how do you access the widgets in the Designer you created are not getting accessible or dont know how to access

    i can tell you some simple example to access them
    especially the slot of the widgets

    if you have created a line edit , then refer to the coding of ui_mainwindow.h, there you will find the slots you have created or you can add your own

    to access the slot like that of textchanged of line edit you created , you may refer it by on_lineedit_textchanged
    and connect it by the line
    connect(lineEdit, SIGNAL(textChanged(const QString &)),
    this, SLOT(on_lineEdit_textChanged()));
    refer the ebooks for better clarification
    hope that helps !!!!!!!!!

  3. #3
    Join Date
    Jul 2009
    Posts
    13
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: General: Structure of a Qt mainwindow program

    For me it is a little difficult to explain the coding in human language, therefore my question is maybe not very clear.

    What I want to know is, how to inherit the ui_mainwindow or mainwindow class so that I have access to all the widget objects instantiated in the setupUi method?

    greetz, kai

  4. #4
    Join Date
    Feb 2008
    Posts
    98
    Thanks
    2
    Thanked 24 Times in 24 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: General: Structure of a Qt mainwindow program

    There are two ways. You may subclass the Ui::MainWindow class or have it as a class member. I personally prefer the first way but it's mostly a matter of taste.

    The first one (the multiple inheritance approach):

    mainwindow.h
    Qt Code:
    1. #include "ui_mainwindow.h"
    2.  
    3. class MainWindow : public QMainWindow, private Ui::MainWindow
    4. {
    5. Q_OBJECT
    6.  
    7. public:
    8. MainWindow(QWidget *parent = 0);
    9.  
    10. protected slots:
    11. void sayHello();
    12. };
    To copy to clipboard, switch view to plain text mode 

    mainwindow.cpp
    Qt Code:
    1. #include "mainwindow.h"
    2.  
    3. MainWindow::MainWindow(QWidget *parent)
    4. : QMainWindow(parent)
    5. {
    6. setupUi(this);
    7.  
    8. connect(m_myButton, SIGNAL(clicked()),
    9. this, SLOT(sayHello()));
    10. }
    11.  
    12. void MainWindow::sayHello()
    13. {
    14. m_myLineEdit->setText(tr("Hello world"));
    15. }
    To copy to clipboard, switch view to plain text mode 

    For the second way (the single inheritance approach):

    mainwindow.h
    Qt Code:
    1. #include "ui_mainwindow.h"
    2.  
    3. class MainWindow : public QMainWindow
    4. {
    5. Q_OBJECT
    6.  
    7. public:
    8. MainWindow(QWidget *parent = 0);
    9.  
    10. protected slots:
    11. void sayHello();
    12.  
    13. private:
    14. Ui::MainWindow ui;
    15. };
    To copy to clipboard, switch view to plain text mode 

    mainwindow.cpp
    Qt Code:
    1. #include "mainwindow.h"
    2.  
    3. MainWindow::MainWindow(QWidget *parent)
    4. : QMainWindow(parent)
    5. {
    6. ui.setupUi(this);
    7.  
    8. connect(ui.m_myButton, SIGNAL(clicked()),
    9. this, SLOT(sayHello()));
    10. }
    11.  
    12. void MainWindow::sayHello()
    13. {
    14. ui.myLineEdit->setText(tr("Hello world"));
    15. }
    To copy to clipboard, switch view to plain text mode 

    More details.
    Last edited by victor.fernandez; 7th September 2009 at 10:13. Reason: added links

  5. #5
    Join Date
    Jul 2009
    Posts
    13
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: General: Structure of a Qt mainwindow program

    Thx a lot for the post. I didn't know that there are two ways of doing it. That clarified a little about my question.

    I used the first way you described, to subclass the Ui::MainWindow. So far, so good.

    Now, I'd like to have a third class, let's say, MainWindowPlot, which is only concerned with the plotting job. Therefore it needs full access to the widgets instantiated in the setupUi method. How can I subclass from the Ui::MainWindow to accomplish this? And, is doing it like this recommended?

    greetz, kai

  6. #6
    Join Date
    Feb 2008
    Posts
    98
    Thanks
    2
    Thanked 24 Times in 24 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: General: Structure of a Qt mainwindow program

    If you need to have full access to the widgets, it might be easier to use the single inheritance approach (the second method). You could create Ui::MainWindow in the heap instead of in the stack and add a method in your class that returns a pointer to it.

    However, I don't recommend doing so. If you just want to read/write the values of the widgets, it would be safer that your main window provides methods to access the values of the widgets instead of letting other classes access the widgets directly. You might provide a value() and setValue() class where you specify the name of the field. For instance:

    Qt Code:
    1. QString MainWindow::value(const QString& fieldName)
    2. {
    3. if(fieldName == "Name")
    4. return m_name->value();
    5. else if(fieldName == "Email")
    6. return m_email->value();
    7. ...
    8. }
    To copy to clipboard, switch view to plain text mode 

    Even better, you may use a QHash<QString, QWidget*> to store the relevant widgets where you can find them by field name:

    Qt Code:
    1. class MainWindow : public QMainWindow, private Ui::MainWindow
    2. {
    3.  
    4. ...
    5.  
    6. private:
    7. QHash<QString, QWidget*> m_fields;
    8. };
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent)
    2. : QMainWindow(parent)
    3. {
    4. setupUi(this);
    5.  
    6. m_fields.insert("Name", m_name);
    7. m_fields.insert("Email", m_email);
    8. }
    9.  
    10.  
    11. QString MainWindow::value(const QString& fieldName)
    12. {
    13. QWidget *widget = m_fields.value(fieldName);
    14. if(!widget)
    15. return QString();
    16.  
    17. if(widget->metaObject()->className() == "QLineEdit") {
    18. QLineEdit *lineEdit = qobject_cast<QLineEdit*>(widget);
    19. return lineEdit->text();
    20. }
    21. ...
    22. }
    To copy to clipboard, switch view to plain text mode 

  7. The following user says thank you to victor.fernandez for this useful post:

    kaizimir (7th September 2009)

  8. #7
    Join Date
    Jul 2009
    Posts
    13
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: General: Structure of a Qt mainwindow program

    Great, thanks for that explanation and the ideas! This is exactly what I was looking for

    I don't understand the code in complete, but with a little time (next weekend) I can figure it out.

Similar Threads

  1. Some advice on how to structure this program?
    By mrwooster in forum Newbie
    Replies: 13
    Last Post: 12th August 2009, 14:24
  2. Compile general C++ program from Qt
    By srinivasj in forum Qt Programming
    Replies: 3
    Last Post: 30th December 2008, 08:19
  3. How to hide mainwindow at program start up
    By palmer in forum Qt Programming
    Replies: 4
    Last Post: 13th September 2008, 14:35
  4. QT MySQL
    By sabeeshcs in forum Newbie
    Replies: 6
    Last Post: 12th January 2007, 04:19

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.