Results 1 to 4 of 4

Thread: Position of widgets within QTreeWidget items

  1. #1
    Join Date
    Oct 2012
    Posts
    3
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows

    Default Position of widgets within QTreeWidget items

    Hi folks,

    I have uploaded a minimal example (TreeItemTest.zip) to point at my specific issue.

    The example program has two items, where one is child of the other. A QPushButton is shown on every QTreeWidgetItem. In addition, when having children, a QPushButton for expanding the tree is shown at the right.

    The two items are shown running the same code (using delegate), but they look different because the expand button is hidden when not having children.

    When clicking the "Left" button, it turns red (just to illustrate that its hit). To check if it is hit, the mouse click is calculated to check if the cursor is at the target.

    And here is the problem, try clicking the "Left" buttons, on the one above at the left side of the "Left" button and then click at the below button at the right side of it. (Thus when clicking the buttons, the clicks are not aligned vertically.) As seen, you will sometimes not get a hit (the button does not change color).
    It seems like the geometry of the button clicked is the geometry of the other button (and visa versa).

    The calculation of the button positions is done in the editorEvent. I have also tried to put this code in the mousePressEvent at the QTreeWidget level without success.

    Could anyone point at what is wrong and show a way to do this correctly. A link to the exact same issue would do, thus I haven't found one yet.

    Big thanks!
    Attached Files Attached Files

  2. #2
    Join Date
    Feb 2008
    Posts
    491
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11
    Thanks
    12
    Thanked 142 Times in 135 Posts

    Default Re: Position of widgets within QTreeWidget items

    Try the following change:
    Qt Code:
    1. bool TestTreeWidgetItemDelegate::editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index)
    2. {
    3. . . .
    4. . . .
    5. if(rectPushLeft.contains(mouse_event->pos()))
    6. {
    7. qDebug() << item->m_strDescription << "rectPushLeft is clicked" << rectPushLeft;
    8. item->UpdatePushLeft();
    9. //return true; move this statement
    10. }
    11. return true; // to here
    12. }
    13. else
    14. {
    15. return false;
    16. }
    17. return false;
    18. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Oct 2012
    Posts
    3
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows

    Default Re: Position of widgets within QTreeWidget items

    Thank you for your answer and that you have looked into my issue, norobro.

    I see that your suggestion is to change the return value when leaving the editorEvent. true means that the editorEvent has handled the event, false means it has not. Your suggestion may be correct, but unfortunately it does not help on this issue.

    Please compile&run the test program and click the mouse several times switching between clicking the positions a) then b) (see attached WhereToClick.png WheretoClick.png) and notice that the buttons not always change color (it is supposed to change on the first click). On the other hand, if clicked on positions where the "Left" buttons are aligned vertically, they will always change color because the calculation of the clicked position in respect of the position of the pushbutton matches.

    Thanks again
    Last edited by dagvegar; 2nd November 2012 at 07:45.

  4. #4
    Join Date
    Feb 2008
    Posts
    491
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11
    Thanks
    12
    Thanked 142 Times in 135 Posts

    Default Re: Position of widgets within QTreeWidget items

    The problem is, with regard to m_pPushExpand being visible or not, that the geometry of m_pPushLeft is not updated until the editor widget is shown. So in your delegate's editorEvent() function, item->GetPushLeftGeometry() returns the previous position of the button.

    I hope that makes sense.

    As a test, try the following:
    Qt Code:
    1. void TestTreeWidgetItem::RefreshWidget(TestTreeWidgetItemEditor *itemeditor, TestTreeWidget::ItemType type)
    2. {
    3. if(type == TestTreeWidget::ITTypeA)
    4. {
    5. itemeditor->m_plabelDescription->setText(m_strDescription);
    6. itemeditor->m_pPushExpand->setVisible((childCount() > 0));
    7.  
    8. QRect rect = itemeditor->m_pPushLeft->geometry();
    9. if(childCount()>0) rect.setX(338);
    10. else rect.setX(380);
    11. itemeditor->m_pPushLeft->setGeometry(rect);
    12.  
    13. itemeditor->m_pPushLeft->setStyleSheet("background: "+ m_strColor +";");
    14. }
    15. }
    To copy to clipboard, switch view to plain text mode 


    Hard coding the position of the button won't work unless you set your main widget to a fixed size.
    Last edited by norobro; 3rd November 2012 at 19:36.

Similar Threads

  1. QTreeWidget, i cant see the items
    By clepto in forum Qt Programming
    Replies: 1
    Last Post: 20th May 2012, 12:15
  2. Replies: 2
    Last Post: 16th December 2010, 12:52
  3. A question about position of widgets
    By Tei in forum Newbie
    Replies: 5
    Last Post: 1st July 2010, 17:35
  4. How to get item position in QTreeWidget?
    By wesley8086 in forum Qt Programming
    Replies: 3
    Last Post: 7th May 2010, 06:28
  5. Position of Items in QGraphicsScene/QGraphicsView
    By StefanK2 in forum Qt Programming
    Replies: 11
    Last Post: 7th July 2009, 15:04

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
  •  
Qt is a trademark of The Qt Company.