Results 1 to 2 of 2

Thread: Place widgets at specific position

  1. #1
    Join Date
    Nov 2019
    Lyon, France
    Qt products

    Default Place widgets at specific position


    I have a very quick question.

    To make things simple, the data my users will load will have some sort of events at specific time.
    Each of this event is associated with a code.
    I'm already able to draw a vertical line at the time of the event, and now, I want to display those codes on top of my plot at the specified time, above the lines.

    Here's what I have for now:
    Qt Code:
    1. void EventLabels::displayEvents(QVector<QVector<int>> eventToDisplay)
    2. {
    3. // eventToDisplay:
    4. // Event Position | Event Code
    6. for (int i = 0; i < eventToDisplay.length(); i++)
    7. {
    8. QLabel *eventCode = new QLabel;
    9. eventCode->setText(QString::number(eventToDisplay[i][1]));
    11. eventCode->setParent(this);
    13. eventCode->move(eventToDisplay[0][1], 0);
    14. }
    15. }
    To copy to clipboard, switch view to plain text mode 

    I have to note that this function is a slot that receive data from another class. As the user can change the displayed data, the codes will change too each time the user push a button.
    From what I find, this seems to be the best solution to precisely place QWidgets at specific position.
    Did I miss something or is there a better way to do it?

  2. #2
    Join Date
    Jan 2008
    Alameda, CA, USA
    Thanked 808 Times in 797 Posts
    Qt products
    Windows Android

    Default Re: Place widgets at specific position

    It is usually never a good idea to place widgets at fixed locations. What happens when the user decides to resize the window? All of your labels are locked in position relative to the top left corner of the parent widget, so they won't move and will now be in the wrong places. Because you create them without storing their pointers anywhere (except as children of the parent widget), it is difficult to retrieve them and move them to the correct positions.

    If this was my problem, I would make a custom QWidget with a paintEvent method. In the paintEvent, you calculate the positions of where your labels should be based on the current size of the widget, and draw them using QPainter::drawText(). Don't use QLabels as child widgets at all. If you use this custom QWidget correctly and put it inside of a layout, it will be resized (and repainted) automatically when the size changes. Simply store your vector of text locations as a member variable of this class, and add a slot to update it when the user changes the labels.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. Replies: 1
    Last Post: 12th May 2013, 21:17
  2. QGridLayout place new widgets at top
    By rouge in forum Newbie
    Replies: 7
    Last Post: 6th October 2011, 08:06
  3. qwebframe - scrolling to a specific position
    By boast in forum Qt Programming
    Replies: 1
    Last Post: 15th June 2010, 02:20
  4. How correct to place widgets in QMdiSubWindow
    By sawerset in forum Qt Programming
    Replies: 0
    Last Post: 15th April 2009, 23:49
  5. Replies: 1
    Last Post: 18th July 2006, 12:06


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.