Results 1 to 4 of 4

Thread: Real size in pixels of a widget

  1. #1
    Join Date
    Oct 2007
    Posts
    7
    Thanked 1 Time in 1 Post

    Default Real size in pixels of a widget

    Hello all,

    I have a QGraphicsView on my main window, whose geometry is reported in QtDesigner to have a width of 579 pixels. However, when I ask for its width in code:

    win.mapView.width()
    - or -
    win.mapView.geometry().width()

    I get the value 100. Why is that, and how can I get the real value?

    Thanks.

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Real size in pixels of a widget

    Hi. The size of a widget is calculated when shown for the first time. Before that you will get bogus values unless the size has been explicitly set. Could this be the case for you? Are you asking for width() for example in a constructor?
    J-P Nurmi

  3. #3
    Join Date
    Oct 2007
    Posts
    7
    Thanked 1 Time in 1 Post

    Default Re: Real size in pixels of a widget

    Yes, that's it. calling win.show() before using width() solves the problem. Boy, it's been a long time since I've done GUI programming :-)

  4. #4
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    258
    Thanks
    22
    Thanked 19 Times in 16 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Real size in pixels of a widget

    This simple app calculates the width of a QGraphicsView and displays it in a statusbar and updates the value every time the size changes. Check against your own code to see what's wrong.

    Qt Code:
    1. #include <QApplication>
    2. #include <QtGui>
    3.  
    4. class MainWindow : public QMainWindow
    5. {
    6. Q_OBJECT
    7. public:
    8. MainWindow(QWidget* parent=0) : QMainWindow(parent) {
    9. view = new QGraphicsView;
    10. view->installEventFilter(this);
    11. setCentralWidget(view);
    12.  
    13. }
    14. bool eventFilter(QObject* obj, QEvent* event) {
    15. if (obj == view && event->type() == QEvent::Resize)
    16. statusBar()->showMessage(QString::number(view->width()));
    17. return QMainWindow::eventFilter(obj, event);
    18. }
    19. private:
    20.  
    21.  
    22. };
    23.  
    24. #include "main.moc"
    25. int main(int argc, char* argv[])
    26. {
    27. QApplication app(argc, argv);
    28. MainWindow mw;
    29. mw.show();
    30. return app.exec();
    31. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Simple custom widget won't size properly
    By MrGarbage in forum Qt Tools
    Replies: 2
    Last Post: 9th August 2007, 13:12
  2. Adjusting Widget size in a QGridLayout
    By Max Yaffe in forum Qt Programming
    Replies: 1
    Last Post: 30th July 2007, 22:03
  3. Need to know widget size before first shown
    By durbrak in forum Qt Programming
    Replies: 1
    Last Post: 11th January 2007, 20:37
  4. Size of a plugin widget in designer
    By high_flyer in forum Qt Programming
    Replies: 4
    Last Post: 28th February 2006, 13:29
  5. Qt 4.1.1 linker warnings
    By Matt Smith in forum Installation and Deployment
    Replies: 0
    Last Post: 26th February 2006, 22:14

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.