PDA

View Full Version : Pusbutton height and width



seany
5th June 2013, 19:02
Hi all

There is something fundamental I am not getting in Qt widget height and width. I am trying programatically to have a QTextEdit text_field and QPushButton button be aligned horizontally on the size of the button plus some (maybe text_field +10% more from width and height of button).

To do that, first I put the 2 widgets in a layout and add the layout to a window. The problem is that the button shows a very large size - in fact it is the same size as
the text_field. Below is the Qdebug output

Button height/width 480 640
Text_field height/width 480 640


However, when displayed, it shows:

9104

As seen, the Button is far smaller than the text_field. My question is why is the actual height and width of the button not shown in button.height() and button.width()? Where do I actually get the actual height and widthj of the button then ? My hope is to use these dimensions to set the height and width of the the text_field (*1.10 for 10 % increase).

Note, even if I comment out text_field, the height & width of button is still 480 640. I have tried getting the Qsize of button and from there to get it's Qsize.height and Qsize.width to the same effect. I also added a Horizontal Layout and put the two widgets in a GroupBox and added that to the layout, hoping it would help automatically size to no effect. I am not showing the code here as I have reduced it to its bare minimum.

I have attached the code below. Appreciate your advice.

Thanks

Sean



-------------------------------Code---------------------------------------------------------
#include <QApplication>
#include <QLabel>
#include <QPushButton>
#include <QtGui>
#include <QtCore>
#include <QtWidgets>

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

QWidget *window = new QWidget;
QGridLayout *layout = new QGridLayout;
QPushButton *button = new QPushButton();
QTextEdit *text_field = new QTextEdit();
window->setWindowTitle("My App");

layout->addWidget(text_field, 0, 0);
layout->addWidget(button, 0, 1);
window->setLayout(layout);
qDebug() << "Button height/width " << button->height() << " " << button->width();
qDebug() << "Text_field height/width " << text_field->height() << " " << text_field->width();
window->show();

return a.exec();
}

----------------- Profile -----------------------------------------------------

#-------------------------------------------------
#
# Project created by QtCreator 2013-06-05T08:52:32
#
#-------------------------------------------------

QT += core gui widgets

TARGET = Test_Screen

TEMPLATE = app

SOURCES += main.cpp

seany
6th June 2013, 04:11
I changed QTextEdit to QLineEdit and at least the text and button are now aligned. It is still showing the same sizes :

Button height/width 480 640
Text_field height/width 480 640

So I still dont know why the sizes are not the real sizes.

Sean

karankumar1609
6th June 2013, 06:38
You have used the layout to set your buttons, but somehow layout stretch child widgets.
In this case either use horizontal or vertical spacer which is needed.

If you still searching the answer then go to Qt Designer and create your own .ui file.
You will got your answer.

Size policy also matters in the stretching the size of widgets.

CHEERS

ChrisW67
6th June 2013, 07:17
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>

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"

seany
6th June 2013, 18:25
Thanks Chris.

I see the qDebug correctly now:

Button height/width QSize(32, 23)
Text_field height/width QSize(256, 192)

Expanding on this thread, I haven't tried any sophisticated manipulation sizes of widgets yet but it seems counter intuitive that setting the width/height of objects is not actually viewable until the object is actually displayed. If my premise is correct, there seems to be a need to have extra fields in QObject or QWidget which shows actual size as it is set ?

Thanks

Sean

ChrisW67
7th June 2013, 03:10
You are not setting the size of widgets, the Qt layout managers are. They use widget size hints and constraints that you can set and are known before display. The Qt layout managers cannot know how much actual space they have to divide up until the operating system window manager places the window on the display. The window manager will size the window according to whatever constraints it has, including hints provided by Qt, and set a size that drives the available client area for Qt to divide amongst sub-widgets.

seany
7th June 2013, 07:35
Thanks.

Sean