Results 1 to 9 of 9

Thread: QGroupBox Issues

  1. #1
    Join Date
    Jan 2006
    Location
    Ohio
    Posts
    332
    Thanks
    37
    Thanked 8 Times in 4 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Question QGroupBox Issues

    I have converted my Qt 4.2 project down to Qt 3.3 as I was tasked. Of course nothing appeared like it did in my Qt 4.2 project so now it's time to debug and get it back to the way it was, but at least I can compile and get something to display. In my Main GUI, I have a label at the top-right, label in the bottom-left and a QSplitter in the middle. On the right side of the splitter I plan for 4 QGroupBoxes and on the left one QTable. Right now I am trying to get my 4 group boxes to show, and the middle two boxes are empty in the beginning...stuff is entered into them based on user interactions later on. The first problem I have is I don't see any titles on the QGroupBoxes and some of the members of the bottom QGroupBox is in between members of the top QGroupBox. And since I can't see any QGroupBox titles, I don't know if my empty boxes are being drawn. Here are some snippets of my code:

    Qt Code:
    1. Inside my function MainWindow::createGUI
    2. QFrame *mainFrame = new QFrame( this );
    3. QVBoxLayout *mainLayout = new QVBoxLayout( mainFrame );
    4. QSplitter *mainSplitter = new QSplitter( Qt::Horizontal );
    5.  
    6. QFrame *constraintFrame = new QFrame( mainSplitter );
    7. QVBoxLayout *constraintLayout = new QVBoxLayout( constraintFrame );
    8.  
    9. ...
    10.  
    11. createBox1( );
    12. createBox2( );
    13. createBox3( );
    14. createBox4( );
    15.  
    16. constraintLayout->addWidget( mpBox1 );
    17. constraintLayout->addWidget( mpBox2 );
    18. constraintLayout->addWidget( mpBox3 );
    19. constraintLayout->addWidget( mpBox4 );
    20.  
    21. ...
    22.  
    23. setCentralWidget( mainFrame );
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. bool MainWindow::createBox1( )
    2. {
    3. mpBox1 = new QGroupBox( tr( "Display Constraints" ) );
    4. QGridLayout *layout1 = new QGridLayout( mpBox1 );
    5. QLabel *collectorsLabel = new QLabel( tr( "Collectors" ), this );
    6. QLabel *minLabel = new QLabal( tr( "Min1:" ), this );
    7. QLabel *maxLabel = new QLabel( tr( "Max1:" ), this );
    8. mCollectorsBox = new QComboBox( this );
    9. mMinLineEdit = new QLineEdit( this );
    10. mMaxLineEdit = new QLineEdit( this );
    11.  
    12. mCollectorsBox->insertItem( tr( "1" ) );
    13. mCollectorsBox->insertItem( tr( "2" ) );
    14. mCollectorsBox->insertItem( tr( "3" ) );
    15. mCollectorsBox->insertItem( tr( "4" ) );
    16. mCollectorsBox->insertItem( tr( "5" ) );
    17.  
    18. layout1->addMultiCellWidget( collectorsLabel, 0, 0, 0, 1 );
    19. layout1->addMultiCellWidget( mCollectorsBox, 0, 0, 3, 3 );
    20. layout1->addMultiCellWidget( minLabel, 1, 1, 0, 0, Qt::AlignCenter );
    21. layout1->addMultiCellWidget( mMinLineEdit, 1, 1, 1, 3 );
    22. layout1->addMultiCellWidget( maxLabel, 2, 2, 0, 0, Qt::AlignCenter );
    23. layout1->addMultiCellWidget( mMaxLineEdit, 2, 2, 1, 3 );
    24.  
    25. return true;
    26. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. bool MainWindow::createBox4( )
    2. {
    3. mpBox4 = new QGroupBox( tr( "Section Constraints" ) );
    4. QGridLayout *layout4 = new QGridLayout( mpBox4 );
    5. QLabel *minLabel = new QLabal( tr( "Min2:" ), this );
    6. QLabel *maxLabel = new QLabel( tr( "Max2:" ), this );
    7. mMinSectionLineEdit = new QLineEdit( this );
    8. mMaxSectionLineEdit = new QLineEdit( this );
    9.  
    10. layout1->addMultiCellWidget( minLabel, 0, 0, 0, 0, Qt::AlignCenter );
    11. layout1->addMultiCellWidget( mMinSectionLineEdit, 0, 0, 1, 1 );
    12. layout1->addMultiCellWidget( maxLabel, 1, 1, 0, 0, Qt::AlignCenter );
    13. layout1->addMultiCellWidget( mMaxSectionLineEdit, 1, 1, 1, 1 );
    14.  
    15. return true;
    16. }
    To copy to clipboard, switch view to plain text mode 

    Any of the members that begin with "mp" are member-pointers of my class and any object that begins with "m" is a class member with the exception of minLabal and maxLabel. When I run the program the main part of my window looks like:

    collectorsLabel mCollectorsBox
    Min2: mMinSectionLineEdit


    Min1: mMinLineEdit


    Max2: mMaxSectionLineEdit
    Max1: mMaxLineEdit


    My plan is for it to look like:

    GroupBox1
    collectorsLabel mCollectorsBox
    Min1: mMinLineEdit
    Max1: mMaxLineEdit

    GroupBox2

    GroupBox3

    GroupBox4
    Min2: mMinSectionLineEdit
    Max2: mMaxSectionLineEdit


    Like I said GroupBox2 and GroupBox3 are empty when I first load the program but the boxes should still appear. Can you see anything from my code as to what I could be doing incorrectly? Thanks!

  2. #2
    Join Date
    Jan 2006
    Location
    Cambridge, MA
    Posts
    32
    Thanked 7 Times in 6 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QGroupBox Issues

    QGroupBoxes are particularly nasty in Qt3. They were rewritten for Qt4 to work in the sane manner of "Create QGroupBox and set any layout you want". This was NOT the usage in t Qt3.

    In Qt3 a GroupBox had an automatic layout assigned. QGroupBox by itself was a grid, QVGroupBox was a QVBoxLayout and QHBoxGroup had a QHBoxLayout. Infact there is really two layouts for each of these internally required to avoid widgets over the frame and label.

    The best thing to do to make your code readable and reusable is to do something like this:

    Qt Code:
    1. QVGroupBox* box = new QVGroupBox(usualParent);
    2. QWidget* blankWidget = new QWidget(box);
    3. // blankWidget has been auto added to the group box's layout
    4.  
    5. QGridLayout* gridLayout = new QGridLayout(blankWidget);
    6. QComboBox* widgetApprearingInBox = new QComboBox(blankWidget);
    7. gridLayout->addWidget(0,0,widgetApprearingInBox);
    To copy to clipboard, switch view to plain text mode 

    Its awkward, its confusing, it was rewritten for Qt4.... Try to convice your employer that Qt4 is far superior (and supportable).

    --Justin

  3. #3
    Join Date
    Jan 2006
    Location
    Ohio
    Posts
    332
    Thanks
    37
    Thanked 8 Times in 4 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Red face Re: QGroupBox Issues

    I changed my createBox1 functions like this:

    Qt Code:
    1. bool MainWindow::createBox1( )
    2. {
    3. // mpBox1 = new QGroupBox( tr( "Display Constraints" ) );
    4. layout1 = new QVGroupBox( tr( "Display Constraints" ) );
    5. QWidget *dummyWidget = new QWidget( mpVGroupBox );
    6.  
    7. // QGridLayout *layout1 = new QGridLayout( mpBox1 );
    8. QGridLayout *layout1 = new QGridLayout( dummyWidget );
    9.  
    10. QLabel *collectorsLabel = new QLabel( tr( "Collectors" ), this );
    11. QLabel *minLabel = new QLabal( tr( "Min1:" ), this );
    12. QLabel *maxLabel = new QLabel( tr( "Max1:" ), this );
    13. mCollectorsBox = new QComboBox( this );
    14. mMinLineEdit = new QLineEdit( this );
    15. mMaxLineEdit = new QLineEdit( this );
    16.  
    17. mCollectorsBox->insertItem( tr( "1" ) );
    18. mCollectorsBox->insertItem( tr( "2" ) );
    19. mCollectorsBox->insertItem( tr( "3" ) );
    20. mCollectorsBox->insertItem( tr( "4" ) );
    21. mCollectorsBox->insertItem( tr( "5" ) );
    22.  
    23. layout1->addMultiCellWidget( collectorsLabel, 0, 0, 0, 1 );
    24. layout1->addMultiCellWidget( mCollectorsBox, 0, 0, 3, 3 );
    25. layout1->addMultiCellWidget( minLabel, 1, 1, 0, 0, Qt::AlignCenter );
    26. layout1->addMultiCellWidget( mMinLineEdit, 1, 1, 1, 3 );
    27. layout1->addMultiCellWidget( maxLabel, 2, 2, 0, 0, Qt::AlignCenter );
    28. layout1->addMultiCellWidget( mMaxLineEdit, 2, 2, 1, 3 );
    29.  
    30. return true;
    31. }
    To copy to clipboard, switch view to plain text mode 

    I changed all of my boxes to look like this and I still don't see any box headers. I do get the items to sort of group together but some of them are off the screen or don't appear but the ones that do appear are in the correct order. I tried changing the parent of all the items to go into the QGridLayout to dummyWidget but then nothing at all appeared on the screen so at least I get some output with using this as the parent. Any other ideas? I have tried to get them to let me use Qt 4 but all they say is No.....

  4. #4
    Join Date
    Jan 2006
    Location
    Cambridge, MA
    Posts
    32
    Thanked 7 Times in 6 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QGroupBox Issues

    How does this even compile? Aside from misspellings like "QLabal", layout1 is used twice with different types

    Qt Code:
    1. // mpBox1 = new QGroupBox( tr( "Display Constraints" ) );
    2. layout1 = new QVGroupBox( tr( "Display Constraints" ) );
    3.  
    4. QWidget *dummyWidget = new QWidget( mpVGroupBox );
    5. // QGridLayout *layout1 = new QGridLayout( mpBox1 );
    6. QGridLayout *layout1 = new QGridLayout( dummyWidget );
    7.  
    8. QLabel *collectorsLabel = new QLabel( tr( "Collectors" ), this );
    9. QLabel *minLabel = new QLabal( tr( "Min1:" ), this );
    To copy to clipboard, switch view to plain text mode 

    You will need to set the parent of all things appearing inside the groupbox to dummyWidget. Make sure your parent/child relationships are right. That is what usually cause widget not to show up. I notice that layout1 = new QVGroupBox( tr( "Display Constraints" ) ); does not include a parent. Use parents in the constructor for all widgets and layouts in Qt3. It's required.

    --Justin

  5. #5
    Join Date
    Jan 2006
    Location
    Ohio
    Posts
    332
    Thanks
    37
    Thanked 8 Times in 4 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QGroupBox Issues

    Sorry the computer I develop on is not hooked up to the internet so a typo is very easy to occur when I retype the code onto here. The first layout1 should be mpBox1 as it was in the commented line before it and yes QLabel is spelled wrong here but is correct on my developing machine. I will try to fix your ideas and see if that helps. I am also experimenting with the designer to see how they write their code. Thanks again!

  6. #6
    Join Date
    Jan 2006
    Location
    Ohio
    Posts
    332
    Thanks
    37
    Thanked 8 Times in 4 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Smile Re: QGroupBox Issues

    I decided to re-write the createGUI function and slowly debug the Qt 3 code so I can efficiently learn how to write Qt 3 code. Here is what my 2 functions look like.

    Qt Code:
    1. bool MainWindow::createGUI
    2. {
    3. setCentralWidget( new QWidget( this ) );
    4.  
    5. QFrame *mainFrame = new QFrame( centralWidget( ) );
    6.  
    7. QWidget *layoutWidget = new QWidget( mainFrame );
    8. QVBoxLayout *mainLayout = new QVBoxLayout( layoutWidget );
    9.  
    10. mpTopLabal = new QLabel( tr( "Hello" ), layoutWidget );
    11. mpTopLabel->setAlignment( Qt::AlignRight );
    12. mpTopLabel->setMaximumHeight( 30 );
    13. mainLayout->addWidget( mpTopLabel );
    14.  
    15. QSplitter *mainSplitter = new QSPlitter( QSplitter::Horizontal, layoutWidget );
    16.  
    17. QFrame *constraintFrame = new QFrame( mainSplitter );
    18. QWidget *constraintWidget = new QWidget( constraintFrame );
    19. QVBoxLayout *constraintLayout = new QVBoxLayout( constraintWidget );
    20.  
    21. mpDisplayBox = new QVGroupBox( tr( "Display Constraints:" ), constraintWidget );
    22. createDisplayBox( );
    23. constraintLayout->addWidget( mpDisplayBox );
    24.  
    25. // I will work with the table later but I want to add the 2nd item to the splitter.
    26. // A splitter with just one item seems pretty useless and I will have a 2nd item
    27. // once the left side is done.
    28. QFrame *tableFrame = new QFrame( mainSplitter );
    29.  
    30. mainLayout->addWidget( mainSplitter );
    31.  
    32. mpBottomLabel = new QLabel( tr( "Bye" ), layoutWidget );
    33. mpBottomLabel->setAlignment( Qt::AlignLeft );
    34. mpBottomLabel->setMaximumHeight( 30 );
    35. mainLayout->addWidget( mpBottomLabel );
    36.  
    37. return true;
    38. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. bool MainWindow::createDisplayBox( )
    2. {
    3. QWidget *dummyWidget = new QWidget( mpDisplayBox );
    4. QGridLayout *displayLayout = new QGridLayout( dummyWidget );
    5.  
    6. QLabel *collectorsLabel = new QLabel( tr( "Collectors: ), dummyWidget );
    7. QLabel *minLabal = new QLabel( tr( "Min:" ), dummyWidget );
    8. QLabel *maxLabel = new QLabel( tr( "Max:" ), dummyWidget );
    9. mpCollectorsBox = new QComboBox( dummyWidget );
    10. mpMinLineEdit = new QLineEdit( dummyWidget );
    11. mpMaxLineEdit = new QLineEdit( dummyWidget );
    12.  
    13. mpCollectorsBox->insertItem( tr( "1" ) );
    14. mpCollectorsBox->insertItem( tr( "2" ) );
    15. mpCollectorsBox->insertItem( tr( "3" ) );
    16. mpCollectorsBox->insertItem( tr( "4" ) );
    17. mpCollectorsBox->insertItem( tr( "5" ) );
    18.  
    19. displayLayout->addMultiCellWidget( collectorsLabel, 0, 0, 0, 1 );
    20. displayLayout->addMutliCellWidget( mpCollectorsBox, 0, 0, 3, 3 );
    21. displayLayout->addMutliCellWidget( minLabel, 1, 1, 0, 0 );
    22. displayLayout->addMutliCellWidget( mpMinLineEdit, 1, 1, 1, 3 );
    23. displayLayout->addMutliCellWidget( maxLabel, 2, 2, 0, 0 );
    24. displayLayout->addMutliCellWidget( mpMaxLineEdit, 2, 2, 1, 3 );
    25.  
    26. displayLayout->setColStretch( 0, 0 );
    27. displayLayout->setColStretch( 1, 5 );
    28. displayLayout->setColStretch( 2, 5 );
    29.  
    30. return true;
    31. }
    To copy to clipboard, switch view to plain text mode 

    The only thing I see on my window is "Hello" which is right-aligned and immediately below it is the label "Bye" which is left-aligned. There is a small dot in between the two labels and I can see the splitter-icon and click and drag it but nothing is appearing inside the splitter which I believe my QGroupBox should be drawing inside. It's as if my GroupBox is not being drawn. Can you see what I am doing wrong? I feel like I am getting a better grasp on Qt 3 and can say I enjoy programming in Qt 4 on my own time better than this stuff! Thanks yet again!

    Edit: I have also noticed that this area is just a tiny portion in the top-left of the QMainWindow. After I call createGUI( ) in my constructor I call resize( 800, 600 ) but the GUI doesn't seem to notice. How do I get mainFrame and all its children to recognize that they should fill the entire QMainWindow and not just a tiny portion in the top-left? If I resize the QMainWindow manually when the program is running, the window resizes but my labels don't seem to be affected by it at all. I am guessing this is another improvement from Qt 3 to Qt 4 but how I do I get it to behave the way I want in Qt 3? Thanks again!
    Last edited by ToddAtWSU; 12th January 2007 at 15:25.

  7. #7
    Join Date
    Jan 2006
    Location
    Cambridge, MA
    Posts
    32
    Thanked 7 Times in 6 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QGroupBox Issues

    setCentralWidget( new QWidget( this ) )

    Here in lies your problem. While Qt4 has a better API than Qt3, Qt3 was a very good toolkit. The issues you are seeing are problems with not following the Qt3 rules. This program would also be broken in Qt4.

    The problem is your central widget has no layout and this does not know how to layout its childern. The children by default are bieing left in the upper lefthand corner. Same thing would happen in Qt4. You want to remove the above line and change:
    QFrame *mainFrame = new QFrame( centralWidget( ) ) to
    QFrame *mainFrame = new QFrame( this )

    At the end of your createUI add
    setCentralWidget(mainFrame);

    Make sure every widget has a parent AND is added to a layout.

    --Justin

  8. The following user says thank you to Glitch for this useful post:

    ToddAtWSU (16th January 2007)

  9. #8
    Join Date
    Jan 2006
    Location
    Ohio
    Posts
    332
    Thanks
    37
    Thanked 8 Times in 4 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QGroupBox Issues

    Thanks. I now realize that QtDesigner could do setCentralWidget( new QWidget( this ) ) because they use setGeometry( ) for everything. I made the changes you suggested (and how I had done it in Qt4) but I still don't see much changes. I resize my QMainWindow to 800x600 but stuff stays in the top-left. However, if I manually call resize( ) on mainFrame and layoutWidget and set them to 800x600 I start to see a lot more of my GUI. Is there a reason why I have to manually call resize to get my widgets to resize or is there a way to get the widgets to resize to fit the window automatically like they did in Qt4? Thanks again!

  10. #9
    Join Date
    Jan 2006
    Location
    Ohio
    Posts
    332
    Thanks
    37
    Thanked 8 Times in 4 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QGroupBox Issues

    I will post my last resize question under a different topic since it really has nothing to do with the groupboxes. Thanks for all your help!

Similar Threads

  1. time and date issues
    By boog07005 in forum Newbie
    Replies: 5
    Last Post: 20th August 2012, 15:15
  2. QGroupBox title truncated on both sides
    By Gopala Krishna in forum Qt Programming
    Replies: 2
    Last Post: 7th October 2006, 15:02
  3. MySql plugin driver issues
    By stevey in forum Installation and Deployment
    Replies: 11
    Last Post: 20th September 2006, 13:45
  4. Issues regarding QMySql drivers and mysql database
    By bera82 in forum Qt Programming
    Replies: 2
    Last Post: 10th August 2006, 17:50
  5. how to change QGroupBox frame color? (Qt4)
    By high_flyer in forum Qt Programming
    Replies: 7
    Last Post: 9th May 2006, 13:48

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.