Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Hi, a need help with QTableView and QAction using dynamically
Forum Updated to NodeBB v4.3 + New Features

Hi, a need help with QTableView and QAction using dynamically

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 2 Posters 1.1k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Allan5A Offline
    Allan5A Offline
    Allan5
    wrote on last edited by aha_1980
    #1

    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();
                }
            }
        }
    }
    

    0_1542862129370_bb427d6a-2246-4ed6-aa57-a01587616d69-image.png

    0_1542862178254_4d26a673-e32a-472e-b9cf-2609b69a21d5-image.png

    1 Reply Last reply
    0
    • dheerendraD dheerendra

      Try with this.
      tableWid->setContextMenuPolicy(Qt::ActionsContextMenu);

      Allan5A Offline
      Allan5A Offline
      Allan5
      wrote on last edited by
      #5

      @dheerendra

      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.

      1 Reply Last reply
      0
      • dheerendraD Offline
        dheerendraD Offline
        dheerendra
        Qt Champions 2022
        wrote on last edited by
        #2

        Did you try adding QAction objects using addAction method for QTableView ?

        Dheerendra
        @Community Service
        Certified Qt Specialist
        http://www.pthinks.com

        Allan5A 1 Reply Last reply
        1
        • dheerendraD dheerendra

          Did you try adding QAction objects using addAction method for QTableView ?

          Allan5A Offline
          Allan5A Offline
          Allan5
          wrote on last edited by
          #3

          @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

          1 Reply Last reply
          0
          • dheerendraD Offline
            dheerendraD Offline
            dheerendra
            Qt Champions 2022
            wrote on last edited by dheerendra
            #4

            Try with this.
            tableWid->setContextMenuPolicy(Qt::ActionsContextMenu);

            Dheerendra
            @Community Service
            Certified Qt Specialist
            http://www.pthinks.com

            Allan5A 1 Reply Last reply
            6
            • dheerendraD dheerendra

              Try with this.
              tableWid->setContextMenuPolicy(Qt::ActionsContextMenu);

              Allan5A Offline
              Allan5A Offline
              Allan5
              wrote on last edited by
              #5

              @dheerendra

              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.

              1 Reply Last reply
              0
              • dheerendraD Offline
                dheerendraD Offline
                dheerendra
                Qt Champions 2022
                wrote on last edited by
                #6
                ```
                

                act = new QAction("PthinkS");
                tableWid->addAction(act);
                connect(act,SIGNAL(triggered(bool)),this,SLOT(callMe()));

                void MyWidget::callMe() {
                qDebug() << Q_FUNC_INFO << endl;
                }

                Dheerendra
                @Community Service
                Certified Qt Specialist
                http://www.pthinks.com

                1 Reply Last reply
                1

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved