Results 1 to 6 of 6

Thread: customizing widget layout: best practice

  1. #1
    Join Date
    Sep 2010
    Posts
    18
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default customizing widget layout: best practice

    Dear friends,

    I created UI for widget using QtDesigner, and now I want to customize it a little bit before showing to the user. So I'm trying to do this in the widget's constructor:
    Qt Code:
    1. public:
    2. MyWidget(QWidget *_parent) : QWidget(_parent)
    3. {
    4. ui.setupUi(this);
    5.  
    6. ....
    7. // For example: let's move the checkbox
    8. QPoint p1 = ui.checkBox1->mapToGlobal(QPoint(0, 0));
    9. QPoint p2 = ui.checkBox2->mapToGlobal(QPoint(0, 0));
    10. ui.checkBox->setStyleSheet(QString("margin-left:%1px;").arg(p1.x()-p2.x())); // oops
    11. ...
    12. }
    To copy to clipboard, switch view to plain text mode 

    But here I face the problem: p1 and p2 are just the same.

    So, I'm wondering:

    1. Why they are the same?

    2. Is it possible to perform such customization in widget's constructor? I know that "QTimer::singleShot(0, this, SLOT(moveMyCheckbox() ))" will solve this, but I'm not sure if it is not a "dirty hack".

    3. What is the best way to do such things?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: customizing widget layout: best practice

    Quote Originally Posted by stillwaiting View Post
    1. Why they are the same?
    Because the position of a widget without an explicitly set geometry is not determined until the widget is shown for the first time.

    2. Is it possible to perform such customization in widget's constructor? I know that "QTimer::singleShot(0, this, SLOT(moveMyCheckbox() ))" will solve this, but I'm not sure if it is not a "dirty hack".
    I think what you are trying to do in general is a dirty hack. What exactly is the purpose for trying to set the stylesheet?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Sep 2010
    Posts
    18
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: customizing widget layout: best practice

    Hi wysota and thank you for the reply.

    What exactly is the purpose for trying to set the stylesheet?
    I have groupbox with checkboxes in it and another one checkbox below. And I'm trying to put all checkboxes (include the one which is outside) on the same vertical. Screenshot is attached.

    But actually I'm wondering what is the best way to do such kind of things. When I have to do something while constructing the widget, but to do this I must know layouts positions/sizes/child object's positions/sizes etc. This is just a particular case.
    Attached Images Attached Images

  4. #4
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: customizing widget layout: best practice

    for that better put the single check box also in a box where you set the title "" and use the flat option. If that does not work for you put it in a frame or widget or even only in a layout where you manipulate the left spacing.

  5. #5
    Join Date
    Sep 2010
    Posts
    18
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: customizing widget layout: best practice

    for that better put the single check box also in a box where you set the title "" and use the flat option
    Hmm... sounds like a good idea, thank you But can I be sure that the "flat" QGroupBox has the same indents as the "not flat" one?

    or even only in a layout where you manipulate the left spacing.
    As I understand the situation: groupbox border thickness could be different on different platforms and/or could vary depending on OS settings. So, I think, that constant left margin here could play a bad joke.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: customizing widget layout: best practice

    Quote Originally Posted by stillwaiting View Post
    As I understand the situation: groupbox border thickness could be different on different platforms and/or could vary depending on OS settings. So, I think, that constant left margin here could play a bad joke.
    I'd say trying to align the checkboxes is as a bad idea as setting a constant left margin, which is what your stylesheet is trying to do anyway. If the OS style says checkboxes in groups and out of groups should be aligned, they will be aligned. If it says there is no such need, they won't be aligned so don't try and force that. I'm usually against "I know best what is good for my users" approach and I don't see why this situation would differ.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. mainwindow layout vs widget layout
    By fatecasino in forum Newbie
    Replies: 2
    Last Post: 14th December 2010, 14:45
  2. Replies: 1
    Last Post: 8th February 2010, 04:58
  3. Best practice when handling a slow loading widget?
    By TheNewGuy in forum Qt Programming
    Replies: 1
    Last Post: 4th December 2009, 06:12
  4. Customizing a Widget's Tool Tip.
    By Cutey in forum Qt Programming
    Replies: 1
    Last Post: 24th July 2008, 08:08
  5. customizing table widget
    By krishna.bv in forum Qt Programming
    Replies: 1
    Last Post: 25th January 2007, 13:43

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.