Results 1 to 7 of 7

Thread: Pusbutton height and width

  1. #1
    Join Date
    Jun 2013
    Posts
    11
    Thanks
    1
    Qt products
    Qt5
    Platforms
    MacOS X Windows

    Default Pusbutton height and width

    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:

    test.jpg

    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

  2. #2
    Join Date
    Jun 2013
    Posts
    11
    Thanks
    1
    Qt products
    Qt5
    Platforms
    MacOS X Windows

    Default Re: Pusbutton height and width

    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

  3. #3
    Join Date
    Feb 2013
    Location
    India
    Posts
    153
    Thanks
    27
    Thanked 18 Times in 18 Posts
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Pusbutton height and width

    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

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Pusbutton height and width

    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:
    Qt Code:
    1. #include <QApplication>
    2. #include <QTextEdit>
    3. #include <QPushButton>
    4. #include <QGridLayout>
    5. #include <QWidget>
    6. #include <QDebug>
    7.  
    8. #include <QTimer>
    9.  
    10. class Window: public QWidget
    11. {
    12. Q_OBJECT
    13.  
    14. QPushButton *button;
    15. QTextEdit *text_field;
    16.  
    17. public:
    18. Window(QWidget *p = 0): QWidget(p) {
    19. setWindowTitle("My App");
    20. QGridLayout *layout = new QGridLayout(this);
    21. button = new QPushButton(this);
    22. text_field = new QTextEdit(this);
    23.  
    24. layout->addWidget(text_field, 0, 0);
    25. layout->addWidget(button, 0, 1);
    26. setLayout(layout);
    27.  
    28. QTimer::singleShot(0, this, SLOT(report())); // will fire when the program becomes "idle" after being show()n
    29. }
    30. public slots:
    31. void report() {
    32. qDebug() << "Button height/width" << button->size();
    33. qDebug() << "Text_field height/width" << text_field->size();
    34. }
    35. };
    36.  
    37. int main(int argc, char *argv[])
    38. {
    39. QApplication a(argc, argv);
    40. Window w;
    41. w.show();
    42. return a.exec();
    43. }
    44. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 

  5. The following user says thank you to ChrisW67 for this useful post:

    seany (6th June 2013)

  6. #5
    Join Date
    Jun 2013
    Posts
    11
    Thanks
    1
    Qt products
    Qt5
    Platforms
    MacOS X Windows

    Default Re: Pusbutton height and width

    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

  7. #6
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Pusbutton height and width

    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.

  8. #7
    Join Date
    Jun 2013
    Posts
    11
    Thanks
    1
    Qt products
    Qt5
    Platforms
    MacOS X Windows

    Default Re: Pusbutton height and width

    Thanks.

    Sean

Similar Threads

  1. Widget Height and Width
    By in_dbasu in forum Qt Programming
    Replies: 3
    Last Post: 11th August 2011, 08:44
  2. Fix proportion of width and height in QGridLayout
    By Wong in forum Qt Programming
    Replies: 6
    Last Post: 7th January 2011, 07:24
  3. Replies: 3
    Last Post: 13th May 2010, 08:20
  4. QPushButton set width and height from StyleSheet
    By AL in forum Qt Programming
    Replies: 3
    Last Post: 19th March 2010, 14:33
  5. width and height of QTabWidget
    By chikkireddi in forum Qt Programming
    Replies: 6
    Last Post: 29th October 2007, 13:53

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.