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.