Results 1 to 7 of 7

Thread: General: Structure of a Qt mainwindow program

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #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

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
  •  
Qt is a trademark of The Qt Company.