seany
5th June 2013, 18: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
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