PDA

View Full Version : Problem in enabling QtreeWidget items dynamically



jyoti kumar
31st May 2006, 07:10
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 -

QFlags<Qt::ItemFlag> flag(Qt::ItemIsEnabled);
flag = flag | Qt::ItemIsSelectable;

//CPSSRouterInterface::getUniqueInstance(m_uiMainWin dow)->runRouter();

QList<QTreeWidgetItem *> widgetList = m_uiMainWindow.routingFlowTree->findItems(QString("View Routing Solution"),Qt::MatchRecursive);
if (widgetList.size() > 0 )
{
widgetList.takeFirst()->setFlags(flag);
}

widgetList = m_uiMainWindow.routingFlowTree->findItems(QString("Reports"),Qt::MatchRecursive);
if (widgetList.size() > 0 )
{
widgetList.takeFirst()->setFlags(flag);
}

widgetList = m_uiMainWindow.routingFlowTree->findItems(QString("Routing Statistics"),Qt::MatchRecursive);
if (widgetList.size() > 0 )
{
widgetList.takeFirst()->setFlags(flag);
}

widgetList = m_uiMainWindow.routingFlowTree->findItems(QString("Routing Solution Cell View"),Qt::MatchRecursive);
if (widgetList.size() > 0 )
{
widgetList.takeFirst()->setFlags(flag);
}

widgetList = m_uiMainWindow.routingFlowTree->findItems(QString("Routing Solution Net View"),Qt::MatchRecursive);
if (widgetList.size() > 0 )
{
widgetList.takeFirst()->setFlags(flag);
}

//m_uiMainWindow.routingFlowTree->setFocus();
m_uiMainWindow.routingFlowTree->repaint();
CFacade::getUniqueInstance()->repaint();

munna
31st May 2006, 07:19
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 )

jyoti kumar
31st May 2006, 07:58
I have already set the Widget items thru Qt Designer

jyoti kumar
1st June 2006, 05:02
Still its not working, pls help

munna
1st June 2006, 05:05
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 )

jyoti kumar
1st June 2006, 06:17
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

munna
1st June 2006, 06:34
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


Moreover my system is crashing using this cmd.


Can we see the code?

jyoti kumar
1st June 2006, 08:26
The code is already present in the first request.

jpn
1st June 2006, 08:50
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.



#include <QtGui>

class TreeWidget: public QTreeWidget
{
Q_OBJECT
public:
TreeWidget(QWidget* parent = 0): QTreeWidget(parent) {
setColumnCount(1);
for (int i = 0; i < 10; ++i) {
QTreeWidgetItem* item = new QTreeWidgetItem(this);
item->setText(0, QString::number(i));
}
};

public slots:
void enableAll() {
for (int i = 0; i < topLevelItemCount(); ++i) {
QTreeWidgetItem* item = topLevelItem(i);
item->setFlags(item->flags() | Qt::ItemIsEnabled);
}
reset(); // ..OR setDirtyRegion(rect());
}

void disableAll() {
for (int i = 0; i < topLevelItemCount(); ++i) {
QTreeWidgetItem* item = topLevelItem(i);
item->setFlags(item->flags() & ~Qt::ItemIsEnabled);
}
reset(); // ..OR setDirtyRegion(rect());
}
};

int main(int argc, char *argv[]) {
QApplication a(argc, argv);

// a tree
TreeWidget* tree = new TreeWidget;

// enable/disable buttons
QVBoxLayout* vbox = new QVBoxLayout;
QPushButton* enable = new QPushButton("Enable");
QPushButton* disable = new QPushButton("Disable");
vbox->addWidget(enable);
vbox->addWidget(disable);
QWidget* buttons = new QWidget;
buttons->setLayout(vbox);

// connect signals
a.connect(enable, SIGNAL(clicked()), tree, SLOT(enableAll()));
a.connect(disable, SIGNAL(clicked()), tree, SLOT(disableAll()));
a.connect(&a, SIGNAL(lastWindowClosed()), &a, SLOT(quit()));

// show 'em all together
QSplitter* splitter = new QSplitter;
splitter->addWidget(tree);
splitter->addWidget(buttons);
splitter->show();

// enter to the event loop
return a.exec();
}

#include "main.moc"

jyoti kumar
7th June 2006, 10:59
But the widget itens are getting highlighted for other piece of code


if(pPSSFacadeState->isProjectSelected())
{
QString openFileName = QFileDialog::getOpenFileName(
this,
"Add Design File",
QDir::currentPath(),
"Design (*.rlc)");
// QMessageBox::information(this,tr("Flow"),openFileName);
if(!openFileName.isEmpty())
{
string designFileFullName = openFileName.toStdString();
int lastDelim = designFileFullName.find_last_of("/");
string designFileName = designFileFullName.substr(lastDelim + 1,designFileFullName.size());

designFileName = CRouterOptions::getUniqueInstance()->getProjectLocation() + "/" + designFileName;

//copy the input file in prj dir
QFile::copy(openFileName,QString(designFileName.c_ str()));
pRouterOptions->setDesignFileName(designFileName);

CPSSFacadeState::getUniqueInstance()->setDesignAdded();


// QMessageBox::information(this,tr("Flow"),tr("After copy of design"));

QFlags<Qt::ItemFlag> flag(Qt::ItemIsEnabled);
flag = flag | Qt::ItemIsSelectable;
QList<QTreeWidgetItem *> widgetList = m_uiMainWindow.routingFlowTree->findItems(QString("Open Design"),Qt::MatchRecursive);

if (widgetList.size() > 0 )
{
widgetList.takeFirst()->setFlags(flag);
}

widgetList = m_uiMainWindow.routingFlowTree->findItems(QString("View Design"),Qt::MatchRecursive);
if (widgetList.size() > 0 )
{
widgetList.takeFirst()->setFlags(flag);
}

widgetList = m_uiMainWindow.routingFlowTree->findItems(QString("Routing"),Qt::MatchRecursive);
if (widgetList.size() > 0 )
{
widgetList.takeFirst()->setFlags(flag);
}

widgetList = m_uiMainWindow.routingFlowTree->findItems(QString("Run Routing"),Qt::MatchRecursive);
if (widgetList.size() > 0 )
{
widgetList.takeFirst()->setFlags(flag);
}
// QMessageBox::information(this,tr("Flow"),tr("After 1"));


m_uiMainWindow.actionDesign_File_2->setEnabled(true);
// QMessageBox::information(this,tr("Flow"),tr("After 2"));
m_pRouterPropDialog->setDefaultOutputFileNames();
// QMessageBox::information(this,tr("Flow"),tr("After enabling tree items"));


//disable Tree items after routing
/*widgetList = m_uiMainWindow.routingFlowTree->findItems(QString("View Routing Solution"),Qt::MatchRecursive);
if (widgetList.size() > 0 )
{
widgetList.takeFirst()->setFlags(false);
}

widgetList = m_uiMainWindow.routingFlowTree->findItems(QString("Reports"),Qt::MatchRecursive);
if (widgetList.size() > 0 )
{
widgetList.takeFirst()->setFlags(false);
}

widgetList = m_uiMainWindow.routingFlowTree->findItems(QString("Routing Statistics"),Qt::MatchRecursive);
if (widgetList.size() > 0 )
{
widgetList.takeFirst()->setFlags(false);
}

widgetList = m_uiMainWindow.routingFlowTree->findItems(QString("Routing Solution Cell View"),Qt::MatchRecursive);
if (widgetList.size() > 0 )
{
widgetList.takeFirst()->setFlags(false);
}

widgetList = m_uiMainWindow.routingFlowTree->findItems(QString("Routing Solution Net View"),Qt::MatchRecursive);
if (widgetList.size() > 0 )
{
widgetList.takeFirst()->setFlags(false);
}*/
// QMessageBox::information(this,tr("Flow"),tr("After disabling tree items"));

m_uiMainWindow.actionRun->setEnabled(true);
// m_uiMainWindow.actionStop->setEnabled(true);
m_uiMainWindow.actionProperties->setEnabled(true);
m_uiMainWindow.actionPSS_Flow->setEnabled(false);
pPSSFacadeState->setFlowFreezed();

/*QCursor cursor;
cursor.setShape(Qt::WaitCursor);
QApplication::setOverrideCursor(cursor);*/
// QMessageBox::information(this,tr("Flow"),tr("after setting tree inputs"));

m_uiMainWindow.outputPane->append(QString("Reading inputs..."));
//emit readRouterInputs();
// QMessageBox::information(this,tr("Add Design"), tr("Before strat router input thread"));
startRouterInputThread();
//CPSSRouterInterface::getUniqueInstance(m_uiMainWin dow)->processRoutingFlowInputs();
}
else
{
QMessageBox::information(this, tr("Add Design Error"), tr("Enter valid Design"));
}
}//end of if(isProjectSelected)

Chicken Blood Machine
7th June 2006, 18:10
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?