Results 1 to 12 of 12

Thread: Lots of code in QMainWindow

  1. #1
    Join Date
    Aug 2008
    Location
    Texas, USA
    Posts
    44
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Lots of code in QMainWindow

    I have to preference this with I'm still a beginner/intermediate to Qt however I am beginning to do more advanced applications. One thing I'm beginning to realize is that lots of code could potentially be placed in the derived class of yours that represents the QMainWindow.

    This is what I mean....Right now I have a QTabWidget and a QDockWidget. Each one of those controls get stuffed into the main Ui class so if say i had 10 buttons on each tab and I had 10 Tabs that would be 100 potentially separate functions (slots) for handling them. Same goes for the QDockWidget.

    Am I wrong in this or is there a way to segment up your code so you don't end up with just one huge 5000 line file. I guess I'm coming from an MFC background where each Tab of the Tab control would be a separate derived class.

    Thanks in advance!

  2. #2
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Lots of code in QMainWindow

    No one prevents you from creating custom classes that represent a special button, a panel, a dock widget... you name it.

    This highly recommended, because you do not want files with 50k lines of code.

  3. #3
    Join Date
    Aug 2008
    Location
    Texas, USA
    Posts
    44
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Lots of code in QMainWindow

    So just so I understand this a Tab for example....

    I would create a new QWidget class of my own that would handle all the contents on that particular Tab, complete with its own form file. Then on the MainWindow form file I would "Promote To" my new QWidget class.

    Is that right?

    Thanks!

  4. #4
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Lots of code in QMainWindow

    There's no need to promote unless you want to modify existing functionality.

    For tabs however, there's nothing wrong with having each tab in a different ui file and source file. Same goes for other parts of your ui such as groupboxes. Its more modular that way. As long as your widgets have a parent, they'll be happy.

  5. #5
    Join Date
    Aug 2008
    Location
    Texas, USA
    Posts
    44
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Lots of code in QMainWindow

    Thanks,

    I guess my problem is that I do not have an understanding of how I would assign a different UI file to a tab in designer (if you can)?

    I'll keep looking. Thanks again.

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

    Default Re: Lots of code in QMainWindow

    Quote Originally Posted by GimpMaster View Post
    I guess my problem is that I do not have an understanding of how I would assign a different UI file to a tab in designer (if you can)?
    You can't do it from Designer. You have to make a proper widget out of each ui file and then use QTabWidget::addTab() to add them to your main widget or use promotion facilities of Designer (but you will still have to create proper widget classes from each ui file).
    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.


  7. #7
    Join Date
    Aug 2008
    Location
    Texas, USA
    Posts
    44
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Lots of code in QMainWindow

    Ok thanks.

  8. #8
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Lots of code in QMainWindow

    I create a seperate UI file for each tab, place a QWidget for each tab on the main form as a holder, and then tab->SetupUI(Widget). No promotion or subclassing necessary, and the source code and widgets for each tab is completely seperate.

    Might not be the "proper" way of doing it, but hey, it works

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

    Default Re: Lots of code in QMainWindow

    Quote Originally Posted by fatjuicymole View Post
    I create a seperate UI file for each tab, place a QWidget for each tab on the main form as a holder, and then tab->SetupUI(Widget). No promotion or subclassing necessary, and the source code and widgets for each tab is completely seperate.
    Please don't do that. You'll encounter more problems with it than benefits. Make proper subclasses from those widgets and either use the promote facility of Designer or add tabs to the tab widget manually. It's really worth it. Otherwise once you start writing some code logic you'll again have everything in one file.
    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.


  10. #10
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Lots of code in QMainWindow

    Quote Originally Posted by wysota View Post
    Please don't do that. You'll encounter more problems with it than benefits. Make proper subclasses from those widgets and either use the promote facility of Designer or add tabs to the tab widget manually. It's really worth it. Otherwise once you start writing some code logic you'll again have everything in one file.
    I don't think you understand what I mean. This is what I mean:

    Qt Code:
    1. [window.cpp]
    2. tab1->Setup(tab1holder);
    3. tab2->Setup(tab2holder);
    4.  
    5. where tabXholder is a QWidget*
    6.  
    7. [tab1.cpp]
    8. Tab::Tab(QWidget *Parent) {
    9. ui->SetupUI(Parent);
    10. connect(...)
    11. connect(...)
    12. }
    13.  
    14. ... handlers for widgets in this tab ...
    To copy to clipboard, switch view to plain text mode 

    Is this style of coding not advised?

    I'm not sure how to subclass in designer when all you have is a group of controls in a widget with no container, or is the first thing you do supposed to be a container before you placed the controls?

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

    Default Re: Lots of code in QMainWindow

    Tell me, how come after posting 66 times on this forum you don't know what Designer is for and what it is not for?

    Qt Code:
    1. class Tab : public QWidget {
    2. public:
    3. Tab(QWidget *parent = 0): QWidget(parent){
    4. ui = new Ui::SomeUi;
    5. ui->setupUi(this);
    6. }
    7. ~Tab() { delete ui; }
    8. private:
    9. Ui::SomeUi *ui;
    10. };
    To copy to clipboard, switch view to plain text mode 

    What's the point of having a class that actually does something on some other object inside its own constructor?
    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.


  12. #12
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Lots of code in QMainWindow

    I never thought of doing it that way

    Why write code when designer can do it for you indeed. Thanks (It also looks neater)

Similar Threads

  1. copyright and gpl
    By janus in forum General Discussion
    Replies: 8
    Last Post: 21st October 2008, 01:13
  2. Qt 4.3.0 lots of Bugs!
    By VireX in forum Qt Programming
    Replies: 69
    Last Post: 20th June 2007, 22:05
  3. Problem closing a QMainWindow in Qt4.2
    By ian in forum Qt Programming
    Replies: 11
    Last Post: 17th October 2006, 00:49
  4. problem with linking
    By mickey in forum Qt Programming
    Replies: 49
    Last Post: 12th August 2006, 21:41

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.