PDA

View Full Version : I can not adjust the size of a widget to your content.



jjcarles
29th March 2010, 18:40
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



#include <QApplication>

#include "mainwindow.h"

int main(int argc, char *argv[])
{
QApplication app(argc, argv);

MyMDI mainMDI;
mainMDI.show();

return app.exec();
}


mainwindow.cpp



#include "mainwindow.h"

#include <QHeaderView>
#include <QMenuBar>

MyTableWidget::MyTableWidget(QWidget *parent) : QWidget(parent)
{
m_tableValues = 0;
m_layout = 0;
}

void MyTableWidget::showData()
{
QStringList headers;
headers << tr("C1") << tr("C2") << ("C3");

int maxrows = 300;

m_tableValues = new QTableWidget(maxrows, 3, this);
m_layout = new QGridLayout(this);

m_tableValues->verticalHeader()->hide();
m_tableValues->setHorizontalHeaderLabels(headers);
m_tableValues->verticalHeader()->setUpdatesEnabled(false);

for(int i = 0; i < maxrows; i++) {
QTableWidgetItem *newItem = new QTableWidgetItem(tr("%L1").arg(1000*i+1));
newItem->setTextColor(Qt::black);
newItem->setTextAlignment(Qt::AlignRight | Qt::AlignVCenter);
m_tableValues->setItem(i, i % 3, newItem);
}

m_tableValues->resizeColumnsToContents();
m_tableValues->resizeRowsToContents();

m_tableValues->setMinimumSize(m_tableValues->minimumSizeHint());

m_tableValues->verticalHeader()->setUpdatesEnabled(true);

m_layout->addWidget(m_tableValues);
}

QSize MyTableWidget::minimumSizeHint() const
{
QSize size;

if(!m_tableValues) {
QTableWidget aux;
size = aux.minimumSizeHint();
return size;
}

int width = 0;
for(int c = 0; c < m_tableValues->columnCount(); c++) {
width += m_tableValues->columnWidth(c);
}

size.setWidth(width+50);
size.setHeight(200);

return size;
}

QSize MyTableWidget::sizeHint() const
{
return minimumSizeHint();
}

/* -------------------------------------------------- */
/* -------------------------------------------------- */
/* -------------------------------------------------- */

MyMdiWindow::MyMdiWindow(QWidget *parent) : QWidget(parent)
{
m_layout = new QVBoxLayout(this);
m_table = new MyTableWidget(this);
m_layout->addWidget(m_table);
setLayout(m_layout);
}

void MyMdiWindow::showMyTableWidget()
{
m_table->showData();
m_table->setMinimumSize(m_table->sizeHint());
}

/* -------------------------------------------------- */
/* -------------------------------------------------- */
/* -------------------------------------------------- */

MyMDI::MyMDI()
{
m_mdiArea = new QMdiArea;
m_mdiArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded );
m_mdiArea->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
setCentralWidget(m_mdiArea);

m_openAct = new QAction(tr("Open"), this);
connect(m_openAct, SIGNAL(triggered()),this, SLOT(open()));

m_Menu = menuBar()->addMenu(tr("&Menu"));
m_Menu->addAction(m_openAct);
}

void MyMDI::open()
{
MyMdiWindow* mdiWindow = new MyMdiWindow(this);
m_mdiArea->addSubWindow(mdiWindow);
mdiWindow->showMyTableWidget();
mdiWindow->show();
}



mainwindow.h



#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QMdiArea>
#include <QAction>
#include <QMenu>
#include <QWidget>
#include <QTableWidget>
#include <QGridLayout>

class MyTableWidget : public QWidget
{
Q_OBJECT

protected:
QTableWidget* m_tableValues;
QGridLayout* m_layout;

public:
MyTableWidget(QWidget* parent = 0);
void showData();

QSize minimumSizeHint() const;
QSize sizeHint() const;
};

/* -------------------------------------------------- */
/* -------------------------------------------------- */
/* -------------------------------------------------- */

class MyMdiWindow : public QWidget
{
Q_OBJECT

protected:
MyTableWidget* m_table;
QVBoxLayout* m_layout;

public:
MyMdiWindow(QWidget *parent = 0);
void showMyTableWidget();

};

/* -------------------------------------------------- */
/* -------------------------------------------------- */
/* -------------------------------------------------- */

class MyMDI : public QMainWindow
{
Q_OBJECT

public:
MyMDI();

private slots:
void open();

private:
QMdiArea *m_mdiArea;

QMenu *m_Menu;
QAction *m_openAct;
QAction *m_exitAct;

};

#endif // MAINWINDOW_H



Thanks in advance.

P.D. Sorry for my English.

ChrisW67
2nd April 2010, 02:45
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:


MyMDI::MyMDI()
MyMDI::open()
MyMdiWindow::MyMdiWindow()
MyTableWidget::MyTableWidget()
MyTableWidget::sizeHint()
MyTableWidget::minimumSizeHint()
returns QSize(61, 61)
returns QSize(61, 61)
MyTableWidget::minimumSizeHint()
returns QSize(61, 61)
MyMdiWindow::showMyTableWidget()
MyTableWidget::showData()
MyTableWidget::sizeHint()
MyTableWidget::minimumSizeHint()
returns QSize(227, 200)
returns QSize(227, 200)
MyTableWidget::sizeHint()
MyTableWidget::minimumSizeHint()
returns QSize(227, 200)
returns QSize(227, 200)
MyTableWidget::minimumSizeHint()
returns QSize(227, 200)
MyTableWidget::sizeHint()
MyTableWidget::minimumSizeHint()
returns QSize(227, 200)
returns QSize(227, 200)
MyTableWidget::minimumSizeHint()
returns QSize(227, 200)


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.