PDA

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



jjcarles
31st March 2010, 09:07
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



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

jjcarles
8th April 2010, 19:03
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.

ChrisW67
8th April 2010, 22:38
What was wrong with the assistance the first time you asked this question?
http://www.qtcentre.org/threads/29394-I-can-not-adjust-the-size-of-a-widget-to-your-content.?p=138094&highlight=#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()

jjcarles
8th April 2010, 22:54
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.