Results 1 to 12 of 12

Thread: QTreeWidget::topLevelItem() results in seg fault.

  1. #1
    Join Date
    Feb 2006
    Posts
    47
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Arrow QTreeWidget::topLevelItem() results in seg fault.

    Originally, I posted about a problem I was having, but got no response:

    I had a QTreeView and wanted to change the color of one of the entries in the view. I opted to change the QTreeView into a QTreeWidget and called QTreeView::setModel() to set my model to match the view.

    While the model-view interface works fine, I seem to get a segmentation fault when I do the following in a member function:

    Qt Code:
    1. void DatabaseTreeWidget::Update_Tree_From_Flags()
    2. {
    3.  
    4. X = topLevelItem(0);
    5. X->setTextColor(1,QColor(Qt::darkGreen));
    6. }
    To copy to clipboard, switch view to plain text mode 

    Why does this happen? I want to change the color of the top level tree item, but am clearly having difficulty.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    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: QTreeWidget::topLevelItem() results in seg fault.

    So are you using a QTreeView object or QTreeWidget?

  3. #3
    Join Date
    Feb 2006
    Posts
    47
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTreeWidget::topLevelItem() results in seg fault.

    I was using a QTreeView, but am now using QTreeWidget.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    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: QTreeWidget::topLevelItem() results in seg fault.

    So... why are you talking about a model?

    Anyway, did you check if X!=0?

  5. #5
    Join Date
    Feb 2006
    Posts
    47
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTreeWidget::topLevelItem() results in seg fault.

    I am using a model with this view.

    QTreeWidget inherits QTreeView. Since QTreeView was limited in what it could do, I changed it to a QTreeWidget calling QTreeView::setModel() where appropriate. This section works like a charm, or at least I can see/manipulate the model in the view without a problem.

    As for checking if X!=0.... I commented out the setTextColor() line, (Line 6.) and see that Line 5 is the offending execution. Any further insight?

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    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: QTreeWidget::topLevelItem() results in seg fault.

    Did you notice that setModel() in QTreeWidget is made private? It is not done this way without a reason, you know...

    The mismatch between the model and the item approach probably cause the segfault.

    In what way is the model approach limited? It has exactly the same capabilities like the item one (especially given the fact that item based widgets are emulated by using an internal model representation).

    For example the code you pasted here in the model approach looks like so:

    Qt Code:
    1. void DatabaseTreeView::Update_Tree_From_Flags(){
    2. QModelIndex ind = model()->index(0,0);
    3. model()->setData(ind, Qt::TextColorRole, Qt::darkGreen);
    4. }
    To copy to clipboard, switch view to plain text mode 

  7. #7
    Join Date
    Feb 2006
    Posts
    47
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTreeWidget::topLevelItem() results in seg fault.

    I switched back to QTreeView to give it another shot.

    Now, I am trying your suggestion (the code section you listed) and do not seem to be getting any sucess. The tree view comes up as before, after the model has been set, and thre is no change in color. As far as I can tell, your suggestion should be straightforward - so why is this not working? Do I need to refresh the view somehow?

  8. #8
    Join Date
    Feb 2006
    Posts
    47
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTreeWidget::topLevelItem() results in seg fault.

    I am now suspecting that it is in fact my model that is not allowing for the color change but am not sure. Could this be possible? Here is my simple implementation, with some irrelevant stuff removed:

    Qt Code:
    1. InternalScanDatabase::InternalScanDatabase(QObject *parent)
    2. {
    3. // Set up the root of the database.
    4. QList<QVariant> rootData;
    5. rootData<<"Scan Directory";//<<"File Path";
    6. rootItem = new ScanDatabaseItem(rootData);
    7. // Make the root the start of the database list.
    8. Database<<rootItem;
    9. }
    10.  
    11. InternalScanDatabase::~InternalScanDatabase()
    12. {
    13. delete rootItem;
    14. Temp.close(); // Debugging only.
    15. }
    16.  
    17. int InternalScanDatabase::columnCount(const QModelIndex &parent) const
    18. {
    19. if (parent.isValid())
    20. return static_cast<ScanDatabaseItem*>(parent.internalPointer())->columnCount();
    21. else
    22. return rootItem->columnCount();
    23. }
    24.  
    25. QVariant InternalScanDatabase::data(const QModelIndex &index, int role) const
    26. {
    27. if (!index.isValid())
    28. return QVariant();
    29.  
    30. if (role != Qt::DisplayRole)
    31. return QVariant();
    32.  
    33. ScanDatabaseItem *item = static_cast<ScanDatabaseItem*>(index.internalPointer());
    34.  
    35. return item->data(index.column());
    36. }
    37.  
    38. Qt::ItemFlags InternalScanDatabase::flags(const QModelIndex &index) const
    39. {
    40. if (!index.isValid())
    41. return Qt::ItemIsEnabled;
    42.  
    43. return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
    44. }
    45.  
    46. QVariant InternalScanDatabase::headerData(int section, Qt::Orientation orientation,
    47. int role) const
    48. {
    49. if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
    50. return rootItem->data(section);
    51.  
    52. return QVariant();
    53. }
    54.  
    55. QModelIndex InternalScanDatabase::index(int row, int column, const QModelIndex &parent)
    56. const
    57. {
    58. ScanDatabaseItem *parentItem;
    59.  
    60. if (!parent.isValid())
    61. parentItem = rootItem;
    62. else
    63. parentItem = static_cast<ScanDatabaseItem*>(parent.internalPointer());
    64.  
    65. ScanDatabaseItem *childItem = parentItem->child(row);
    66. if (childItem)
    67. return createIndex(row, column, childItem);
    68. else
    69. return QModelIndex();
    70. }
    71.  
    72. QModelIndex InternalScanDatabase::parent(const QModelIndex &index) const
    73. {
    74. if (!index.isValid())
    75. return QModelIndex();
    76.  
    77. ScanDatabaseItem *childItem = static_cast<ScanDatabaseItem*>(index.internalPointer());
    78. ScanDatabaseItem *parentItem = childItem->parent();
    79.  
    80. if (parentItem == rootItem)
    81. return QModelIndex();
    82.  
    83. return createIndex(parentItem->row(), 0, parentItem);
    84. }
    85.  
    86. int InternalScanDatabase::rowCount(const QModelIndex &parent) const
    87. {
    88. ScanDatabaseItem *parentItem;
    89.  
    90. if (!parent.isValid())
    91. parentItem = rootItem;
    92. else
    93. parentItem = static_cast<ScanDatabaseItem*>(parent.internalPointer());
    94.  
    95. return parentItem->childCount();
    96. }
    To copy to clipboard, switch view to plain text mode 

  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    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: QTreeWidget::topLevelItem() results in seg fault.

    You need to have data() and setData() properly implemented to retrieve and store those parameters accordingly.

  10. #10
    Join Date
    Feb 2006
    Posts
    47
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTreeWidget::topLevelItem() results in seg fault.

    Hello again,

    My problem is not yet resolved, but feel I am getting closer thanks to your help. In my model, I have done the following (sloppy) coding to see what would happen:

    Qt Code:
    1. QVariant InternalScanDatabase::data(const QModelIndex &index, int role) const
    2. {
    3. if (!index.isValid())
    4. return QVariant();
    5.  
    6. //if (role != Qt::DisplayRole)
    7. // return QVariant();
    8.  
    9. if (role == Qt::TextColorRole)
    10. return Qt::darkGreen;
    11.  
    12. if (role== Qt::DisplayRole)
    13. {
    14. ScanDatabaseItem *item = static_cast<ScanDatabaseItem*>(index.internalPointer());
    15. return item->data(index.column());
    16. }
    17. else return QVariant();
    18. }
    To copy to clipboard, switch view to plain text mode 

    I was thinking that the view class (QTreeView) would call this function with role = Qt::TextColorRole. In this case, I would unconditionally return Qt::darkGreen. What I was expecting was that the entire tree would be green. In reality, this did not happen. Any further insight?

  11. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    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: QTreeWidget::topLevelItem() results in seg fault.

    It should work, at least if you don't use a custom delegate.

    Ah... sure it doesn't work The attached example shows why. Qt::darkGreen is an int, not a QColor -- you need to make sure QColor is returned. Look at the attachment for details.
    Attached Files Attached Files

  12. The following user says thank you to wysota for this useful post:

    johnny_sparx (24th March 2006)

  13. #12
    Join Date
    Feb 2006
    Posts
    47
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTreeWidget::topLevelItem() results in seg fault.

    ah HA!

    Got it. Thanks for everyone's help. It worked like a charm. I also made space in the nodes of my data structrure's tree to store the color. Now I can use setData and modify the role appropriately so that color's can be changed. This was a very useful thing and I learned much about MVP.

    J.

Similar Threads

  1. Can't get QXmlQuery to return results
    By bpetty in forum Newbie
    Replies: 2
    Last Post: 13th May 2008, 15:27
  2. Process aborted. Segmentation fault
    By Pragya in forum Qt Programming
    Replies: 3
    Last Post: 30th May 2007, 09:12
  3. Segmentation fault running any QT4 executables
    By jellis in forum Installation and Deployment
    Replies: 7
    Last Post: 19th May 2007, 17:35
  4. Icons missing => segmentation fault
    By antonio.r.tome in forum Qt Programming
    Replies: 4
    Last Post: 8th March 2006, 17:30

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.