Hi, a need help with QTableView and QAction using dynamically
-
Hi, Here's my code, when double clicked in an item from qlistview i add a new Qtablewidget and a new Qtableview, now my question is, how i add a customContextMenu in my QTableView dynamic.
thank you already.
void MainWindow::on_listTablas_itemDoubleClicked(QListWidgetItem *item) { if(item->isSelected()){ QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE"); db.setDatabaseName(ui->txtRutaDb->text()); for(int i = 0; i < tablasClickadas.count(); i++){ if(tablasClickadas.at(i) == item->text()){ return; } } if(!db.open()){ QMessageBox::warning(this,"Error fatal","Error al abrir base de datos | " + db.lastError().text()); ui->txtRutaDb->setText(""); db.close(); } else{ QSqlTableModel *model = new QSqlTableModel(this); model->setTable(item->text()); if(model->select()){ listModel.append(model); QWidget *widget = new QWidget(ui->tabTablas); QHBoxLayout *box = new QHBoxLayout(widget); QTableView *tableWid = new QTableView(widget); box->addWidget(tableWid); tableWid->setObjectName(QStringLiteral("tableWid")); tableWid->setModel(model); tableWid->setContextMenuPolicy(Qt::CustomContextMenu); this->listTables.append(tableWid); this->tablasClickadas.append(item->text()); ui->tabTablas->addTab(widget,item->text()); db.close(); } else{ QMessageBox::warning(this,"Error", "Error al leer tabla de la base datos..."); db.close(); } } } }
-
Thanks, now function.
Now just one more question, how to control when you click on an option, what slots you should use.
If you can give me an example, it does not matter if I do not equal, I will investigate elsewhere, I thank you for the help.
-
Did you try adding QAction objects using addAction method for QTableView ?
-
@dheerendra said in Hi, a need help with QTableView and QAction using dinamically:
Did you try adding QAction objects using addAction method for QTableView ?
Yes, I use
tableWid->addAction(new QAction("Nuevo registro",tableWid));
but not function, i don't know why not function, it does not show when I right click
-
Try with this.
tableWid->setContextMenuPolicy(Qt::ActionsContextMenu); -
Thanks, now function.
Now just one more question, how to control when you click on an option, what slots you should use.
If you can give me an example, it does not matter if I do not equal, I will investigate elsewhere, I thank you for the help.
-
```
act = new QAction("PthinkS");
tableWid->addAction(act);
connect(act,SIGNAL(triggered(bool)),this,SLOT(callMe()));void MyWidget::callMe() {
qDebug() << Q_FUNC_INFO << endl;
}