Results 1 to 4 of 4

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.

    Hi.

    Previously I put this message in the forum for newbies, but no such luck. So I have decided to change it to this forum. Sorry for the inconvenience.

    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.
    Last edited by jacek; 5th April 2010 at 20:06. Reason: fixed code display

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

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

    Hello again.

    Seeing that there are no answers, I change the question. If at the time of creating a widget is not yet that big are the widgets that make up the form, how I can adjust the size of the widget to your content? The idea is to show a table (QTableWidget) with data loaded from a file and the file is selected from a QFileDialog. Until the file is not loaded, I do not know the number of columns that exist. How I can adjust the width of the widget that contains the QTableWidget (and other widgets) to the width of QTableWidget?

    Thanks in advance.

  3. #3
    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.

    What was wrong with the assistance the first time you asked this question?
    http://www.qtcentre.org/threads/2939...ht=#post138094


    As for the second question. You'd have to calculate the width that you want the QTableWidget to be and have a look at QWidget::setMinimumSize() and the related QWidget::minimumSizeHint()
    Last edited by ChrisW67; 8th April 2010 at 23:47.

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

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

    Sorry. At first I put the question in the newbie forum, but after I changed the question to the programming forum. I wanted to close the thread or delete the message from the newbie forum, but could not. Thanks for the reply and sorry.

Similar Threads

  1. Replies: 1
    Last Post: 2nd April 2010, 02:45
  2. QTabWidget adjust internal Widget to fit full size
    By ^NyAw^ in forum Qt Programming
    Replies: 4
    Last Post: 25th December 2009, 02:20
  3. trying to adjust size of image in a qlabel
    By mmm286 in forum Newbie
    Replies: 1
    Last Post: 5th November 2009, 07:43
  4. How can i make widget automatically adjust screen size
    By kapoorsudhish in forum Qt Programming
    Replies: 3
    Last Post: 23rd October 2009, 16:21
  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.