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[])
{
MyMDI mainMDI;
mainMDI.show();
return app.exec();
}
#include <QApplication>
#include "mainwindow.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
MyMDI mainMDI;
mainMDI.show();
return app.exec();
}
To copy to clipboard, switch view to plain text mode
mainwindow.cpp
#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();
}
#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();
}
To copy to clipboard, switch view to plain text mode
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:
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
#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
To copy to clipboard, switch view to plain text mode
Thanks in advance.
P.D. Sorry for my English.
Bookmarks