Results 1 to 11 of 11

Thread: child widget resize to parent widget

  1. #1
    Join Date
    Dec 2010
    Posts
    5
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default child widget resize to parent widget

    Hello!

    I am trying to force a subclass I made called Spreadsheet to automatically expand to the borders of its parent widget.

    Here is appearing as it is, and the red arrows show what I am trying to achieve :



    I did not post all the code, only the code that I thought was relevant.

    spreadsheet.hpp
    Qt Code:
    1. #ifndef SPREADSHEET_HPP
    2. #define SPREADSHEET_HPP
    3.  
    4. #include <QTableWidget>
    5.  
    6. class Spreadsheet : public QTableWidget
    7. {
    8. Q_OBJECT
    9.  
    10. public:
    11. Spreadsheet(int r, int c, QWidget *parent = 0);
    12.  
    13. private:
    14. QTableWidget* table;
    15. };
    16.  
    17. #endif
    To copy to clipboard, switch view to plain text mode 


    spreadsheet.cc
    Qt Code:
    1. #include <QtGui>
    2.  
    3. #include "spreadsheet.hpp"
    4.  
    5. Spreadsheet::Spreadsheet(int r, int c, QWidget* parent) : QTableWidget(parent)
    6. {
    7. table = new QTableWidget(r,c,this);
    8. }
    To copy to clipboard, switch view to plain text mode 

    mainwindow.cc constructor
    Qt Code:
    1. MainWindow::MainWindow()
    2. {
    3. QWidget* widget = new QWidget;
    4. setCentralWidget(widget);
    5.  
    6. tabWidget = new QTabWidget;
    7. sp = new Spreadsheet(200,5, tabWidget); // sp is of type Spreadsheet
    8.  
    9. tabWidget->addTab(sp, tr("First Tab"));
    10.  
    11. QHBoxLayout* layout = new QHBoxLayout;
    12. layout->setMargin(3);
    13. layout->addWidget(tabWidget);
    14.  
    15. widget->setLayout(layout);
    16.  
    17. createActions();
    18. createMenus();
    19.  
    20. QString message = tr("Begin by opening a previous file ");
    21. statusBar()->showMessage(message);
    22.  
    23. setWindowTitle(tr("Menus"));
    24. }
    To copy to clipboard, switch view to plain text mode 

    How would I go about achieving of what is demonstrated with the arrows?

    Thanks

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: child widget resize to parent widget

    A layout will work

  3. #3
    Join Date
    Dec 2010
    Posts
    5
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: child widget resize to parent widget

    For the main widget? There is already a layout.

    I also had a layout for the spreadsheet inside the tab too; it did not work. I will try again nevertheless.

    I'm just about ready to give up on Qt... I spent about 4-5 trying to figure out this one issue yesterday

  4. #4
    Join Date
    Mar 2010
    Location
    Brazil
    Posts
    39
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: child widget resize to parent widget

    Set the layout to parent widget... A "horizontally" layout maybe works.

  5. #5
    Join Date
    Dec 2010
    Posts
    5
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: child widget resize to parent widget

    I emphasized the parent widged and how I applied a layout to it with "!!!!". I already have a layout for the parent( a horizontal one exactly ):

    Qt Code:
    1. MainWindow::MainWindow()
    2.  
    3. {
    4. !!!!!!->>QWidget* widget = new QWidget;
    5.  
    6. !!!!!->>setCentralWidget(widget);
    7.  
    8.  
    9. tabWidget = new QTabWidget;
    10.  
    11. sp = new Spreadsheet(200,5, tabWidget); // sp is of type Spreadsheet
    12.  
    13. tabWidget->addTab(sp, tr("First Tab"));
    14.  
    15. !!!!!!->>QHBoxLayout* layout = new QHBoxLayout;
    16. layout->setMargin(3);
    17.  
    18. !!!!!!->>layout->addWidget(tabWidget);
    19.  
    20. !!!!!!->>widget->setLayout(layout);
    21.  
    22. createActions();
    23. createMenus();
    24.  
    25. QString message = tr("Begin by opening a previous file ");
    26. statusBar()->showMessage(message);
    27.  
    28.  
    29. setWindowTitle(tr("Menus"));
    30. }
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Mar 2010
    Location
    Brazil
    Posts
    39
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: child widget resize to parent widget

    Look this code:

    Qt Code:
    1. tab = new QWidget();
    2. tab->setObjectName(QString::fromUtf8("tab2"));
    3.  
    4. QHBoxLayout *horizontalLayout = new QHBoxLayout(tab);
    5.  
    6. horizontalLayout->setContentsMargins(0, 0, 0, 0);
    7. horizontalLayout->setObjectName(QString::fromUtf8("horizontalLayout"));
    8.  
    9. textEdit = new codeArea(tab);
    10.  
    11. textEdit->setObjectName(QString::fromUtf8("textEdit"));
    12.  
    13. horizontalLayout->addWidget(textEdit);
    14.  
    15. ui->tabWidget->addTab(tab, "Code");
    To copy to clipboard, switch view to plain text mode 

    Here I have the tabWidget and created a tab called tab of type QWidget...

  7. #7
    Join Date
    Dec 2010
    Posts
    5
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: child widget resize to parent widget

    Thank you for working with me so far

    I tried that(HelderC code) and still get the identical issue. I think I better understand the issue : if you go and change Spreadsheet to QTableWidget, everything is fine. All the spacing and formatting is correct. As soon as I change QTableWidget to Spreadsheet, the format stops working.

    I guess it has something to do with the spreadsheet class. I mean, I derived it correctly, right? Something MUST be wrong with the Spreadsheet class but what?

    Qt Code:
    1. class Spreadsheet : public QTableWidget
    2. {
    3.  
    4. Q_OBJECT
    5.  
    6. public:
    7. Spreadsheet(int r, int c, QWidget *parent = 0);
    8.  
    9. private:
    10. QTableWidget* table;
    11.  
    12. };
    13. Spreadsheet::Spreadsheet(int r, int c, QWidget* parent) : QTableWidget(parent)
    14. {
    15. table = new QTableWidget(r,c,this);
    16. }
    To copy to clipboard, switch view to plain text mode 

    I attached all my code.
    EDIT:: Updated file, cleaner code now.
    Attached Files Attached Files
    Last edited by bobFromAccounting; 22nd December 2010 at 15:34.

  8. #8
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: child widget resize to parent widget

    Quote Originally Posted by bobFromAccounting View Post
    Something MUST be wrong with the Spreadsheet class but what?
    You're almost there! Here's a hint: your Spreadsheet class is a QTableWidget.

  9. #9
    Join Date
    Dec 2010
    Posts
    5
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: child widget resize to parent widget

    Quote Originally Posted by norobro View Post
    You're almost there! Here's a hint: your Spreadsheet class is a QTableWidget.
    I thought spreadsheets were supposed to QTableWidgets. What is the issue, kind sir? :P

  10. #10
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: child widget resize to parent widget

    Try this:
    Qt Code:
    1. Spreadsheet::Spreadsheet(int r, int c, QWidget* parent) : QTableWidget(parent)
    2. {
    3. //table = new QTableWidget(r,c,this);
    4. setRowcount(r);
    5. setColumnCount(c);
    6. }
    To copy to clipboard, switch view to plain text mode 
    And comment out the pointer in spreadsheet.hpp

  11. #11
    Join Date
    Feb 2010
    Location
    Sydney, Australia
    Posts
    111
    Thanks
    18
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: child widget resize to parent widget

    One better:

    Qt Code:
    1. Spreadsheet::Spreadsheet(int r, int c, QWidget* parent) : QTableWidget(r, c, parent)
    2. {}
    To copy to clipboard, switch view to plain text mode 

  12. The following user says thank you to stefanadelbert for this useful post:

    gkarthick5 (23rd August 2011)

Similar Threads

  1. Replies: 7
    Last Post: 14th January 2010, 09:47
  2. Replies: 4
    Last Post: 3rd October 2009, 09:19
  3. How to move child widget with parent widget?
    By anupamgee in forum Qt Programming
    Replies: 3
    Last Post: 19th June 2009, 16:23
  4. let parent widget grow with child widget?
    By BeS in forum Qt Programming
    Replies: 3
    Last Post: 17th February 2009, 12:17
  5. Move child widget along with the parent widget
    By sreedhar in forum Qt Programming
    Replies: 2
    Last Post: 15th May 2006, 13:00

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.