Results 1 to 11 of 11

Thread: Problem in enabling QtreeWidget items dynamically

  1. #1
    Join Date
    Apr 2006
    Posts
    28
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Problem in enabling QtreeWidget items dynamically

    Hi,
    Initially I have disabled tree widget items, now I want to enable them after some processing, but the items are not getting enabled unless I minimize & maximize the main window.
    Pls suggest.
    Pls see the code below -
    Qt Code:
    1. QFlags<Qt::ItemFlag> flag(Qt::ItemIsEnabled);
    2. flag = flag | Qt::ItemIsSelectable;
    3.  
    4. //CPSSRouterInterface::getUniqueInstance(m_uiMainWindow)->runRouter();
    5.  
    6. QList<QTreeWidgetItem *> widgetList = m_uiMainWindow.routingFlowTree->findItems(QString("View Routing Solution"),Qt::MatchRecursive);
    7. if (widgetList.size() > 0 )
    8. {
    9. widgetList.takeFirst()->setFlags(flag);
    10. }
    11.  
    12. widgetList = m_uiMainWindow.routingFlowTree->findItems(QString("Reports"),Qt::MatchRecursive);
    13. if (widgetList.size() > 0 )
    14. {
    15. widgetList.takeFirst()->setFlags(flag);
    16. }
    17.  
    18. widgetList = m_uiMainWindow.routingFlowTree->findItems(QString("Routing Statistics"),Qt::MatchRecursive);
    19. if (widgetList.size() > 0 )
    20. {
    21. widgetList.takeFirst()->setFlags(flag);
    22. }
    23.  
    24. widgetList = m_uiMainWindow.routingFlowTree->findItems(QString("Routing Solution Cell View"),Qt::MatchRecursive);
    25. if (widgetList.size() > 0 )
    26. {
    27. widgetList.takeFirst()->setFlags(flag);
    28. }
    29.  
    30. widgetList = m_uiMainWindow.routingFlowTree->findItems(QString("Routing Solution Net View"),Qt::MatchRecursive);
    31. if (widgetList.size() > 0 )
    32. {
    33. widgetList.takeFirst()->setFlags(flag);
    34. }
    35.  
    36. //m_uiMainWindow.routingFlowTree->setFocus();
    37. m_uiMainWindow.routingFlowTree->repaint();
    38. CFacade::getUniqueInstance()->repaint();
    To copy to clipboard, switch view to plain text mode 
    Last edited by wysota; 31st May 2006 at 08:11.

  2. #2
    Join Date
    Jan 2006
    Posts
    667
    Thanks
    10
    Thanked 80 Times in 74 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem in enabling QtreeWidget items dynamically

    In your code you are only setting the flags but you are actually not selecting the items.

    You need to use QTreeWidget::setCurrentItem ( QTreeWidgetItem * item )

  3. #3
    Join Date
    Apr 2006
    Posts
    28
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem in enabling QtreeWidget items dynamically

    I have already set the Widget items thru Qt Designer

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

    Default Re: Problem in enabling QtreeWidget items dynamically

    Still its not working, pls help

  5. #5
    Join Date
    Jan 2006
    Posts
    667
    Thanks
    10
    Thanked 80 Times in 74 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem in enabling QtreeWidget items dynamically

    you have set the item thru the designer but for chaning the selected item you have to find the item you want to select and then call

    QTreeWidget::setCurrentItem ( QTreeWidgetItem * item )

  6. #6
    Join Date
    Apr 2006
    Posts
    28
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem in enabling QtreeWidget items dynamically

    I have to unable multiple Tree widget items. Do I need to use setCurrentItem for all.
    Moreover my system is crashing using this cmd.

    Pls help

  7. #7
    Join Date
    Jan 2006
    Posts
    667
    Thanks
    10
    Thanked 80 Times in 74 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem in enabling QtreeWidget items dynamically

    Quote Originally Posted by jyoti kumar
    I have to unable multiple Tree widget items. Do I need to use setCurrentItem for all.
    Then you might need to use

    void QTreeWidget::setItemSelected ( const QTreeWidgetItem * item, bool select )

    Also, I think you will need to set the appropriate QAbstractItemView::SelectionMode flag

    Quote Originally Posted by jyoti kumar
    Moreover my system is crashing using this cmd.
    Can we see the code?

  8. #8
    Join Date
    Apr 2006
    Posts
    28
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem in enabling QtreeWidget items dynamically

    The code is already present in the first request.

  9. #9
    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: Problem in enabling QtreeWidget items dynamically

    QTreeWidget doesn't seem to provoke any kind of update in consequence of changing item flags. Calling update() or repaint() doesn't work since the view doesn't know about the fact that model has changed in any way. A workaround is to call AbstractItemView::reset() or AbstractItemView::setDirtyRegion() (the latter requires subclassing of QTreeWidget because it's a protected method) after enabling/disabling items.

    Qt Code:
    1. #include <QtGui>
    2.  
    3. class TreeWidget: public QTreeWidget
    4. {
    5. Q_OBJECT
    6. public:
    7. TreeWidget(QWidget* parent = 0): QTreeWidget(parent) {
    8. setColumnCount(1);
    9. for (int i = 0; i < 10; ++i) {
    10. QTreeWidgetItem* item = new QTreeWidgetItem(this);
    11. item->setText(0, QString::number(i));
    12. }
    13. };
    14.  
    15. public slots:
    16. void enableAll() {
    17. for (int i = 0; i < topLevelItemCount(); ++i) {
    18. QTreeWidgetItem* item = topLevelItem(i);
    19. item->setFlags(item->flags() | Qt::ItemIsEnabled);
    20. }
    21. reset(); // ..OR setDirtyRegion(rect());
    22. }
    23.  
    24. void disableAll() {
    25. for (int i = 0; i < topLevelItemCount(); ++i) {
    26. QTreeWidgetItem* item = topLevelItem(i);
    27. item->setFlags(item->flags() & ~Qt::ItemIsEnabled);
    28. }
    29. reset(); // ..OR setDirtyRegion(rect());
    30. }
    31. };
    32.  
    33. int main(int argc, char *argv[]) {
    34. QApplication a(argc, argv);
    35.  
    36. // a tree
    37. TreeWidget* tree = new TreeWidget;
    38.  
    39. // enable/disable buttons
    40. QVBoxLayout* vbox = new QVBoxLayout;
    41. QPushButton* enable = new QPushButton("Enable");
    42. QPushButton* disable = new QPushButton("Disable");
    43. vbox->addWidget(enable);
    44. vbox->addWidget(disable);
    45. QWidget* buttons = new QWidget;
    46. buttons->setLayout(vbox);
    47.  
    48. // connect signals
    49. a.connect(enable, SIGNAL(clicked()), tree, SLOT(enableAll()));
    50. a.connect(disable, SIGNAL(clicked()), tree, SLOT(disableAll()));
    51. a.connect(&a, SIGNAL(lastWindowClosed()), &a, SLOT(quit()));
    52.  
    53. // show 'em all together
    54. QSplitter* splitter = new QSplitter;
    55. splitter->addWidget(tree);
    56. splitter->addWidget(buttons);
    57. splitter->show();
    58.  
    59. // enter to the event loop
    60. return a.exec();
    61. }
    62.  
    63. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  10. #10
    Join Date
    Apr 2006
    Posts
    28
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem in enabling QtreeWidget items dynamically

    But the widget itens are getting highlighted for other piece of code

    Qt Code:
    1. if(pPSSFacadeState->isProjectSelected())
    2. {
    3. QString openFileName = QFileDialog::getOpenFileName(
    4. this,
    5. "Add Design File",
    6. QDir::currentPath(),
    7. "Design (*.rlc)");
    8. // QMessageBox::information(this,tr("Flow"),openFileName);
    9. if(!openFileName.isEmpty())
    10. {
    11. string designFileFullName = openFileName.toStdString();
    12. int lastDelim = designFileFullName.find_last_of("/");
    13. string designFileName = designFileFullName.substr(lastDelim + 1,designFileFullName.size());
    14.  
    15. designFileName = CRouterOptions::getUniqueInstance()->getProjectLocation() + "/" + designFileName;
    16.  
    17. //copy the input file in prj dir
    18. QFile::copy(openFileName,QString(designFileName.c_str()));
    19. pRouterOptions->setDesignFileName(designFileName);
    20.  
    21. CPSSFacadeState::getUniqueInstance()->setDesignAdded();
    22.  
    23.  
    24. // QMessageBox::information(this,tr("Flow"),tr("After copy of design"));
    25.  
    26. QFlags<Qt::ItemFlag> flag(Qt::ItemIsEnabled);
    27. flag = flag | Qt::ItemIsSelectable;
    28. QList<QTreeWidgetItem *> widgetList = m_uiMainWindow.routingFlowTree->findItems(QString("Open Design"),Qt::MatchRecursive);
    29.  
    30. if (widgetList.size() > 0 )
    31. {
    32. widgetList.takeFirst()->setFlags(flag);
    33. }
    34.  
    35. widgetList = m_uiMainWindow.routingFlowTree->findItems(QString("View Design"),Qt::MatchRecursive);
    36. if (widgetList.size() > 0 )
    37. {
    38. widgetList.takeFirst()->setFlags(flag);
    39. }
    40.  
    41. widgetList = m_uiMainWindow.routingFlowTree->findItems(QString("Routing"),Qt::MatchRecursive);
    42. if (widgetList.size() > 0 )
    43. {
    44. widgetList.takeFirst()->setFlags(flag);
    45. }
    46.  
    47. widgetList = m_uiMainWindow.routingFlowTree->findItems(QString("Run Routing"),Qt::MatchRecursive);
    48. if (widgetList.size() > 0 )
    49. {
    50. widgetList.takeFirst()->setFlags(flag);
    51. }
    52. // QMessageBox::information(this,tr("Flow"),tr("After 1"));
    53.  
    54.  
    55. m_uiMainWindow.actionDesign_File_2->setEnabled(true);
    56. // QMessageBox::information(this,tr("Flow"),tr("After 2"));
    57. m_pRouterPropDialog->setDefaultOutputFileNames();
    58. // QMessageBox::information(this,tr("Flow"),tr("After enabling tree items"));
    59.  
    60.  
    61. //disable Tree items after routing
    62. /*widgetList = m_uiMainWindow.routingFlowTree->findItems(QString("View Routing Solution"),Qt::MatchRecursive);
    63.   if (widgetList.size() > 0 )
    64.   {
    65.   widgetList.takeFirst()->setFlags(false);
    66.   }
    67.  
    68.   widgetList = m_uiMainWindow.routingFlowTree->findItems(QString("Reports"),Qt::MatchRecursive);
    69.   if (widgetList.size() > 0 )
    70.   {
    71.   widgetList.takeFirst()->setFlags(false);
    72.   }
    73.  
    74.   widgetList = m_uiMainWindow.routingFlowTree->findItems(QString("Routing Statistics"),Qt::MatchRecursive);
    75.   if (widgetList.size() > 0 )
    76.   {
    77.   widgetList.takeFirst()->setFlags(false);
    78.   }
    79.  
    80.   widgetList = m_uiMainWindow.routingFlowTree->findItems(QString("Routing Solution Cell View"),Qt::MatchRecursive);
    81.   if (widgetList.size() > 0 )
    82.   {
    83.   widgetList.takeFirst()->setFlags(false);
    84.   }
    85.  
    86.   widgetList = m_uiMainWindow.routingFlowTree->findItems(QString("Routing Solution Net View"),Qt::MatchRecursive);
    87.   if (widgetList.size() > 0 )
    88.   {
    89.   widgetList.takeFirst()->setFlags(false);
    90.   }*/
    91. // QMessageBox::information(this,tr("Flow"),tr("After disabling tree items"));
    92.  
    93. m_uiMainWindow.actionRun->setEnabled(true);
    94. // m_uiMainWindow.actionStop->setEnabled(true);
    95. m_uiMainWindow.actionProperties->setEnabled(true);
    96. m_uiMainWindow.actionPSS_Flow->setEnabled(false);
    97. pPSSFacadeState->setFlowFreezed();
    98.  
    99. /*QCursor cursor;
    100.   cursor.setShape(Qt::WaitCursor);
    101.   QApplication::setOverrideCursor(cursor);*/
    102. // QMessageBox::information(this,tr("Flow"),tr("after setting tree inputs"));
    103.  
    104. m_uiMainWindow.outputPane->append(QString("Reading inputs..."));
    105. //emit readRouterInputs();
    106. // QMessageBox::information(this,tr("Add Design"), tr("Before strat router input thread"));
    107. startRouterInputThread();
    108. //CPSSRouterInterface::getUniqueInstance(m_uiMainWindow)->processRoutingFlowInputs();
    109. }
    110. else
    111. {
    112. QMessageBox::information(this, tr("Add Design Error"), tr("Enter valid Design"));
    113. }
    114. }//end of if(isProjectSelected)
    To copy to clipboard, switch view to plain text mode 
    Last edited by wysota; 7th June 2006 at 12:16. Reason: added [code] tags

  11. #11
    Join Date
    Jan 2006
    Location
    Mountain View, CA
    Posts
    279
    Thanked 42 Times in 37 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem in enabling QtreeWidget items dynamically

    Quote Originally Posted by munna
    In your code you are only setting the flags but you are actually not selecting the items.

    You need to use QTreeWidget::setCurrentItem ( QTreeWidgetItem * item )
    He doesn't need to do this. Why would he?
    Save yourself some pain. Learn C++ before learning Qt.

Similar Threads

  1. QTreeWidget problem!!
    By mcenatie in forum Qt Programming
    Replies: 2
    Last Post: 10th March 2006, 11:47

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.