The window does not have a size until after it is shown, which happens when the event loop is reached after calling show(). Your code prints the "size" before it is shown. Try this to see the difference:
#include <QApplication>
#include <QTextEdit>
#include <QPushButton>
#include <QGridLayout>
#include <QWidget>
#include <QDebug>
#include <QTimer>
{
Q_OBJECT
public:
setWindowTitle("My App");
layout->addWidget(text_field, 0, 0);
layout->addWidget(button, 0, 1);
setLayout(layout);
QTimer::singleShot(0,
this,
SLOT(report
()));
// will fire when the program becomes "idle" after being show()n }
public slots:
void report() {
qDebug() << "Button height/width" << button->size();
qDebug() << "Text_field height/width" << text_field->size();
}
};
int main(int argc, char *argv[])
{
Window w;
w.show();
return a.exec();
}
#include "main.moc"
#include <QApplication>
#include <QTextEdit>
#include <QPushButton>
#include <QGridLayout>
#include <QWidget>
#include <QDebug>
#include <QTimer>
class Window: public QWidget
{
Q_OBJECT
QPushButton *button;
QTextEdit *text_field;
public:
Window(QWidget *p = 0): QWidget(p) {
setWindowTitle("My App");
QGridLayout *layout = new QGridLayout(this);
button = new QPushButton(this);
text_field = new QTextEdit(this);
layout->addWidget(text_field, 0, 0);
layout->addWidget(button, 0, 1);
setLayout(layout);
QTimer::singleShot(0, this, SLOT(report())); // will fire when the program becomes "idle" after being show()n
}
public slots:
void report() {
qDebug() << "Button height/width" << button->size();
qDebug() << "Text_field height/width" << text_field->size();
}
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Window w;
w.show();
return a.exec();
}
#include "main.moc"
To copy to clipboard, switch view to plain text mode
Bookmarks