Results 1 to 2 of 2

Thread: I can not adjust the size of a widget to your content.

  1. #1
    Join Date
    Mar 2010
    Posts
    9
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default I can not adjust the size of a widget to your content.

    First, hello to all. My name is Juan Carlos and this is my first message.

    I have an MDI application (QT 4.5.3) that opens a widget that contains a table (QTableWidget) My problem is that the widget does not fit the size of the table when it opens, but if I try to increase the size of the widget, it automatically adjusts to the correct size. What am I doing wrong?

    The problematic code is:

    main.cpp

    Qt Code:
    1. #include <QApplication>
    2.  
    3. #include "mainwindow.h"
    4.  
    5. int main(int argc, char *argv[])
    6. {
    7. QApplication app(argc, argv);
    8.  
    9. MyMDI mainMDI;
    10. mainMDI.show();
    11.  
    12. return app.exec();
    13. }
    To copy to clipboard, switch view to plain text mode 

    mainwindow.cpp

    Qt Code:
    1. #include "mainwindow.h"
    2.  
    3. #include <QHeaderView>
    4. #include <QMenuBar>
    5.  
    6. MyTableWidget::MyTableWidget(QWidget *parent) : QWidget(parent)
    7. {
    8. m_tableValues = 0;
    9. m_layout = 0;
    10. }
    11.  
    12. void MyTableWidget::showData()
    13. {
    14. QStringList headers;
    15. headers << tr("C1") << tr("C2") << ("C3");
    16.  
    17. int maxrows = 300;
    18.  
    19. m_tableValues = new QTableWidget(maxrows, 3, this);
    20. m_layout = new QGridLayout(this);
    21.  
    22. m_tableValues->verticalHeader()->hide();
    23. m_tableValues->setHorizontalHeaderLabels(headers);
    24. m_tableValues->verticalHeader()->setUpdatesEnabled(false);
    25.  
    26. for(int i = 0; i < maxrows; i++) {
    27. QTableWidgetItem *newItem = new QTableWidgetItem(tr("%L1").arg(1000*i+1));
    28. newItem->setTextColor(Qt::black);
    29. newItem->setTextAlignment(Qt::AlignRight | Qt::AlignVCenter);
    30. m_tableValues->setItem(i, i % 3, newItem);
    31. }
    32.  
    33. m_tableValues->resizeColumnsToContents();
    34. m_tableValues->resizeRowsToContents();
    35.  
    36. m_tableValues->setMinimumSize(m_tableValues->minimumSizeHint());
    37.  
    38. m_tableValues->verticalHeader()->setUpdatesEnabled(true);
    39.  
    40. m_layout->addWidget(m_tableValues);
    41. }
    42.  
    43. QSize MyTableWidget::minimumSizeHint() const
    44. {
    45. QSize size;
    46.  
    47. if(!m_tableValues) {
    48. size = aux.minimumSizeHint();
    49. return size;
    50. }
    51.  
    52. int width = 0;
    53. for(int c = 0; c < m_tableValues->columnCount(); c++) {
    54. width += m_tableValues->columnWidth(c);
    55. }
    56.  
    57. size.setWidth(width+50);
    58. size.setHeight(200);
    59.  
    60. return size;
    61. }
    62.  
    63. QSize MyTableWidget::sizeHint() const
    64. {
    65. return minimumSizeHint();
    66. }
    67.  
    68. /* -------------------------------------------------- */
    69. /* -------------------------------------------------- */
    70. /* -------------------------------------------------- */
    71.  
    72. MyMdiWindow::MyMdiWindow(QWidget *parent) : QWidget(parent)
    73. {
    74. m_layout = new QVBoxLayout(this);
    75. m_table = new MyTableWidget(this);
    76. m_layout->addWidget(m_table);
    77. setLayout(m_layout);
    78. }
    79.  
    80. void MyMdiWindow::showMyTableWidget()
    81. {
    82. m_table->showData();
    83. m_table->setMinimumSize(m_table->sizeHint());
    84. }
    85.  
    86. /* -------------------------------------------------- */
    87. /* -------------------------------------------------- */
    88. /* -------------------------------------------------- */
    89.  
    90. MyMDI::MyMDI()
    91. {
    92. m_mdiArea = new QMdiArea;
    93. m_mdiArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded);
    94. m_mdiArea->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
    95. setCentralWidget(m_mdiArea);
    96.  
    97. m_openAct = new QAction(tr("Open"), this);
    98. connect(m_openAct, SIGNAL(triggered()),this, SLOT(open()));
    99.  
    100. m_Menu = menuBar()->addMenu(tr("&Menu"));
    101. m_Menu->addAction(m_openAct);
    102. }
    103.  
    104. void MyMDI::open()
    105. {
    106. MyMdiWindow* mdiWindow = new MyMdiWindow(this);
    107. m_mdiArea->addSubWindow(mdiWindow);
    108. mdiWindow->showMyTableWidget();
    109. mdiWindow->show();
    110. }
    To copy to clipboard, switch view to plain text mode 

    mainwindow.h

    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QMainWindow>
    5. #include <QMdiArea>
    6. #include <QAction>
    7. #include <QMenu>
    8. #include <QWidget>
    9. #include <QTableWidget>
    10. #include <QGridLayout>
    11.  
    12. class MyTableWidget : public QWidget
    13. {
    14. Q_OBJECT
    15.  
    16. protected:
    17. QTableWidget* m_tableValues;
    18. QGridLayout* m_layout;
    19.  
    20. public:
    21. MyTableWidget(QWidget* parent = 0);
    22. void showData();
    23.  
    24. QSize minimumSizeHint() const;
    25. QSize sizeHint() const;
    26. };
    27.  
    28. /* -------------------------------------------------- */
    29. /* -------------------------------------------------- */
    30. /* -------------------------------------------------- */
    31.  
    32. class MyMdiWindow : public QWidget
    33. {
    34. Q_OBJECT
    35.  
    36. protected:
    37. MyTableWidget* m_table;
    38. QVBoxLayout* m_layout;
    39.  
    40. public:
    41. MyMdiWindow(QWidget *parent = 0);
    42. void showMyTableWidget();
    43.  
    44. };
    45.  
    46. /* -------------------------------------------------- */
    47. /* -------------------------------------------------- */
    48. /* -------------------------------------------------- */
    49.  
    50. class MyMDI : public QMainWindow
    51. {
    52. Q_OBJECT
    53.  
    54. public:
    55. MyMDI();
    56.  
    57. private slots:
    58. void open();
    59.  
    60. private:
    61. QMdiArea *m_mdiArea;
    62.  
    63. QMenu *m_Menu;
    64. QAction *m_openAct;
    65. QAction *m_exitAct;
    66.  
    67. };
    68.  
    69. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 

    Thanks in advance.

    P.D. Sorry for my English.

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: I can not adjust the size of a widget to your content.

    At line 108 of mainwindow.cpp you add the widget to the MDI area. At this time the sizeHint() is a default value. The sizeHint() grows when you later call showMyTableWidget()/showData(), but by that time the QMdiSubWindow has sized itself. I put in some qDebug() statements to show the flow:
    Qt Code:
    1. MyMDI::MyMDI()
    2. MyMDI::open()
    3. MyMdiWindow::MyMdiWindow()
    4. MyTableWidget::MyTableWidget()
    5. MyTableWidget::sizeHint()
    6. MyTableWidget::minimumSizeHint()
    7. returns QSize(61, 61)
    8. returns QSize(61, 61)
    9. MyTableWidget::minimumSizeHint()
    10. returns QSize(61, 61)
    11. MyMdiWindow::showMyTableWidget()
    12. MyTableWidget::showData()
    13. MyTableWidget::sizeHint()
    14. MyTableWidget::minimumSizeHint()
    15. returns QSize(227, 200)
    16. returns QSize(227, 200)
    17. MyTableWidget::sizeHint()
    18. MyTableWidget::minimumSizeHint()
    19. returns QSize(227, 200)
    20. returns QSize(227, 200)
    21. MyTableWidget::minimumSizeHint()
    22. returns QSize(227, 200)
    23. MyTableWidget::sizeHint()
    24. MyTableWidget::minimumSizeHint()
    25. returns QSize(227, 200)
    26. returns QSize(227, 200)
    27. MyTableWidget::minimumSizeHint()
    28. returns QSize(227, 200)
    To copy to clipboard, switch view to plain text mode 

    You should look at the order of that methods are called in. Can you populate your table widget before it is shown?

    You could also consider deriving MyTableWidget directly from QTableWidget rather than wrapping it in an otherwise empty QWidget. You may also be able to dispense with the MyMdiWindow class altogether.

Similar Threads

  1. QTabWidget adjust internal Widget to fit full size
    By ^NyAw^ in forum Qt Programming
    Replies: 4
    Last Post: 25th December 2009, 02:20
  2. trying to adjust size of image in a qlabel
    By mmm286 in forum Newbie
    Replies: 1
    Last Post: 5th November 2009, 07:43
  3. How can i make widget automatically adjust screen size
    By kapoorsudhish in forum Qt Programming
    Replies: 3
    Last Post: 23rd October 2009, 16:21
  4. How to adjust QWebView size within a form?
    By richardander in forum Qt Programming
    Replies: 2
    Last Post: 10th June 2009, 10:45
  5. adjust font size to QLabel-size
    By hunsrus in forum Qt Programming
    Replies: 0
    Last Post: 9th July 2008, 15:33

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.