简体   繁体   中英

Selection of QTreeWidgetItem

In this function im trying to connect each QTreeWidgetItem with its corresponding query, once i select an item a Dock widget that has a query result in tableview model is shown.

what i want is that when i select a new item the shown dock widget hide and new one appears with a new result. what i have until now is that each item i select a new Dock widget appears . I want to see only one dock widget.

here is my function

void MainWindow::DocumentTable()
{
tableview = new QTableView;
query = new QSqlQueryModel(this);

foreach(it,treeWidget->selectedItems())
{
    for (int col=0; col< it->columnCount(); ++col)
    {
        qDebug() << col << it->text(col);

QSqlQuery qry;
qry.prepare("select * from document where Folno=:Folno");
qry.bindValue(":Folno", it->text(col));
qry.exec();

query->setQuery(qry);

    tableview->setModel(query);
tableview->setEditTriggers(QAbstractItemView::NoEditTriggers);
tableview->show();

Docwidget= new QDockWidget(this);
Docwidget->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);

Docwidget->setWidget(tableview);
addDockWidget(Qt::RightDockWidgetArea,Docwidget);
Docwidget->show();


if(!query->submit())
  {
     qDebug() << "Error " << query->lastError().text();
  }

 db.close();

}
}
   }

any ideas ?? :)

Not quite sure what you're after, but here's my contribution:

Add a line after:

query = new QSqlQueryModel(this);

then, add this:

QDockWidget * lastDockWidget = NULL;

Add:

if (lastDockWidget != NULL)  // If no null, then
    lastDockWidget.hide();   // hide it, delete it, or whatever...

before

Docwidget= new QDockWidget(this);

and this:

lastDockWidget = Docwidget;

right after:

Docwidget= new QDockWidget(this);

Result:

void MainWindow::DocumentTable()
{
tableview = new QTableView;
query = new QSqlQueryModel(this);
QDockWidget * lastDockWidget = NULL; // This was added

foreach(it,treeWidget->selectedItems())
{
    for (int col=0; col< it->columnCount(); ++col)
    {
        qDebug() << col << it->text(col);

QSqlQuery qry;
qry.prepare("select * from document where Folno=:Folno");
qry.bindValue(":Folno", it->text(col));
qry.exec();

query->setQuery(qry);

    tableview->setModel(query);
tableview->setEditTriggers(QAbstractItemView::NoEditTriggers);
tableview->show();

if (lastDockWidget != NULL)  // This was added
    lastDockWidget.hide();   // This was added
Docwidget= new QDockWidget(this);
lastDockWidget = Docwidget;  // This was added
Docwidget->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);

Docwidget->setWidget(tableview);
addDockWidget(Qt::RightDockWidgetArea,Docwidget);
Docwidget->show();


if(!query->submit())
  {
     qDebug() << "Error " << query->lastError().text();
  }

 db.close();

}
}
   }

Hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM