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
Code:
#include <QApplication>
#include "mainwindow.h"
int main(int argc, char *argv[])
{
MyMDI mainMDI;
mainMDI.show();
return app.exec();
}
mainwindow.cpp
Code:
#include "mainwindow.h"
#include <QHeaderView>
#include <QMenuBar>
{
m_tableValues = 0;
m_layout = 0;
}
void MyTableWidget::showData()
{
headers << tr("C1") << tr("C2") << ("C3");
int maxrows = 300;
m_tableValues->verticalHeader()->hide();
m_tableValues->setHorizontalHeaderLabels(headers);
m_tableValues->verticalHeader()->setUpdatesEnabled(false);
for(int i = 0; i < maxrows; i++) {
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 {
if(!m_tableValues) {
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();
}
/* -------------------------------------------------- */
/* -------------------------------------------------- */
/* -------------------------------------------------- */
{
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
Code:
#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:
public:
MyTableWidget
(QWidget* parent
= 0);
void showData();
QSize minimumSizeHint
() const;
};
/* -------------------------------------------------- */
/* -------------------------------------------------- */
/* -------------------------------------------------- */
{
Q_OBJECT
protected:
MyTableWidget* m_table;
public:
void showMyTableWidget();
};
/* -------------------------------------------------- */
/* -------------------------------------------------- */
/* -------------------------------------------------- */
{
Q_OBJECT
public:
MyMDI();
private slots:
void open();
private:
QMdiArea *m_mdiArea;
};
#endif // MAINWINDOW_H
Thanks in advance.
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.
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()
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.