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. I wanna Add a button in the end of QTableView , i tried these ways but didn't work
Forum Updated to NodeBB v4.3 + New Features

I wanna Add a button in the end of QTableView , i tried these ways but didn't work

Scheduled Pinned Locked Moved Solved General and Desktop
22 Posts 5 Posters 2.6k Views 3 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.
  • M Offline
    M Offline
    MostafaEzzat
    wrote on last edited by MostafaEzzat
    #1
    void MainProcess::on_pushButton_2_clicked()
    {
        QSqlDatabase  Databasee =   QSqlDatabase::addDatabase("QMYSQL" , "thirdConnection");
        Databasee.setHostName("localhost") ;
        Databasee.setUserName("root");
        Databasee.setPassword("mostafa");
        Databasee.setDatabaseName("Clinic_Database");
    
    // Pls Notice that 'QSqlTableModel' doesn't work without making sure that database connected
    
        if(Databasee.open()){
          QMessageBox::about(this,"res","databaseopen") ;
    
    
        }else {
            QMessageBox::about(this,"res","database not open") ;
            qDebug() <<  Databasee.lastError();
        }
    
    
        QSqlTableModel *model = new QSqlTableModel(this,  Databasee);
        model->setTable("PatientsInfo");
        //model->setRelation(6, QSqlRelation("PatientsName", "Age",  "BloodPrint", "PhoneNum", "DiagnosisDisease","consultant");
       model->setEditStrategy(QSqlTableModel::OnManualSubmit);
       model->select();
       model->setHeaderData(0, Qt::Horizontal, QObject::tr("PatientsName"));
       model->setHeaderData(1, Qt::Horizontal, QObject::tr("Age")) ;
       model->setHeaderData(2, Qt::Horizontal, QObject::tr("BloodPrint"));
       model->setHeaderData(3, Qt::Horizontal, QObject::tr("PhoneNum"));
       model->setHeaderData(4, Qt::Horizontal, QObject::tr("DiagnosisDisease"));
       model->setHeaderData(5, Qt::Horizontal, QObject::tr("consultant"));
       model->setHeaderData(6, Qt::Horizontal, QObject::tr("ViewProfile"));
    
       QTableView * view = new QTableView;
       QPushButton* ViewProfile = new QPushButton("ViewProfile");
     
        QSignalMapper *signalMapper = new QSignalMapper(this);
    
       for( int i=0; i<6; i++ ) { //replace rows.length with your list or vector which consists of the data for your rows.
            //do something with your data for normal cells...
            auto item = model->index(i, 6);
         //   model->setData(item, QVariant::fromValue(view->show());
      / * in     model->setData(item, QVariant::fromValue(view->show()); i don't know which object should  refer so i'm not sure about that so i canceled it 
    */
    
            //make new button for this row
            item = model->index(i, 6);
            view->setIndexWidget(item, ViewProfile);
    
            signalMapper->setMapping(ViewProfile, i);
            connect(ViewProfile, SIGNAL(clicked(bool)), signalMapper, SLOT(map()));
       }
       connect(signalMapper, SIGNAL(mapped(int)), this, SLOT(doSomething(int)));
        view->setModel(model);
          view->show();
    /*
    for(int i=0; i<7 ; i++ ) {
     view->setIndexWidget(model->index(1,6), ViewProfile);
    }
    // i tried this way too but it didn't work 
    */
    
    
    
    
    
    
         QString err = model->lastError().text();
    
         if(!model->select())
         {
           QMessageBox::about(this,"erro", err);
    
         }
    
    
    
    }
    
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      What exactly doesn't work ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      M 2 Replies Last reply
      1
      • SGaistS SGaist

        Hi,

        What exactly doesn't work ?

        M Offline
        M Offline
        MostafaEzzat
        wrote on last edited by MostafaEzzat
        #3

        @SGaist Hello , how are you .. the code of the button not added to QTableVIew which is QSingleMapper or only SetIndexWidget but the the other suff like QTableView and QSqlTableModel is working well and showing the data from Database and pls i wanna keep the application working if i closed TheTableView it self coz when i close QTableView it closes the whole Application

        1 Reply Last reply
        0
        • SGaistS SGaist

          Hi,

          What exactly doesn't work ?

          M Offline
          M Offline
          MostafaEzzat
          wrote on last edited by MostafaEzzat
          #4

          @SGaist
          alt text

          This is the programe's Image when QtableView Shown it doesn't have the button and if i closed QtableVIew and it closes the other Application (which is the parent i guess)

          the url of image incase didn't appear "https://ibb.co/522Tvp6"

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            So you are talking about the ViewProfile button ?

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            M 1 Reply Last reply
            0
            • D Offline
              D Offline
              Devopia53
              wrote on last edited by
              #6

              First do view->setModel() and then view->setIndexWidget().

              M 1 Reply Last reply
              2
              • SGaistS SGaist

                So you are talking about the ViewProfile button ?

                M Offline
                M Offline
                MostafaEzzat
                wrote on last edited by
                #7

                @SGaist Yes Exactly , i wanna add a button in the QTableView so i created a column for that which called "ViewProfile" and add buttton in this column or in the end of the Row and if i clicked it view the details of the row so i just wanna add the button

                1 Reply Last reply
                0
                • D Devopia53

                  First do view->setModel() and then view->setIndexWidget().

                  M Offline
                  M Offline
                  MostafaEzzat
                  wrote on last edited by
                  #8

                  @Devopia53 i did that actual , and i tried the two ways of QSingleMapper and only SetIndexWidget but it doesn't work and tried it rightnow too

                  1 Reply Last reply
                  0
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    Something is really not clear here.

                    You have your ViewProfile button and your cartButton button so which one are you talking about exactly ?

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    M 1 Reply Last reply
                    0
                    • SGaistS SGaist

                      Something is really not clear here.

                      You have your ViewProfile button and your cartButton button so which one are you talking about exactly ?

                      M Offline
                      M Offline
                      MostafaEzzat
                      wrote on last edited by MostafaEzzat
                      #10
                      @SGaist 
                      i have tried both but i commented the other piece  of code which is 
                      for(int i=0; i<7 ; i++ ) {
                       view->setIndexWidget(model->index(i,6), ViewProfile);
                      }
                      view->setmodel(model) ; 
                      view->show() ;
                      
                      this is exactly what i've used in the second way .. for the first way i just forgot to edit the name of the button :) and i was gonna edit it if the code worked .. but i'll edit it right now .. and really appreciate your Responding
                      

                      pls in this line : / * in model->setData(item, QVariant::fromValue(view->show());

                      is "View->show()" the right object here ?

                      mrjjM 1 Reply Last reply
                      0
                      • M MostafaEzzat
                        @SGaist 
                        i have tried both but i commented the other piece  of code which is 
                        for(int i=0; i<7 ; i++ ) {
                         view->setIndexWidget(model->index(i,6), ViewProfile);
                        }
                        view->setmodel(model) ; 
                        view->show() ;
                        
                        this is exactly what i've used in the second way .. for the first way i just forgot to edit the name of the button :) and i was gonna edit it if the code worked .. but i'll edit it right now .. and really appreciate your Responding
                        

                        pls in this line : / * in model->setData(item, QVariant::fromValue(view->show());

                        is "View->show()" the right object here ?

                        mrjjM Offline
                        mrjjM Offline
                        mrjj
                        Lifetime Qt Champion
                        wrote on last edited by mrjj
                        #11

                        Hi
                        first thing.

                        for(int i=0; i<7 ; i++ ) {
                         view->setIndexWidget(model->index(i,6), ViewProfile);
                        }
                        

                        You cannot add same button to more than one cell. you have to create a new for each.

                        so it would be

                        for(int i=0; i<7 ; i++ ) {
                         view->setIndexWidget(model->index(i,6), new QPushButton("ViewProfile"));
                        }
                        

                        if you want a button in each cell.


                        Then

                        model->setData(item, QVariant::fromValue(view->show())
                        

                        you try to store what the result of a function call (show is function) but
                        show() returns void so nothing to store.

                        What are you trying to store there ?

                        M 1 Reply Last reply
                        1
                        • mrjjM mrjj

                          Hi
                          first thing.

                          for(int i=0; i<7 ; i++ ) {
                           view->setIndexWidget(model->index(i,6), ViewProfile);
                          }
                          

                          You cannot add same button to more than one cell. you have to create a new for each.

                          so it would be

                          for(int i=0; i<7 ; i++ ) {
                           view->setIndexWidget(model->index(i,6), new QPushButton("ViewProfile"));
                          }
                          

                          if you want a button in each cell.


                          Then

                          model->setData(item, QVariant::fromValue(view->show())
                          

                          you try to store what the result of a function call (show is function) but
                          show() returns void so nothing to store.

                          What are you trying to store there ?

                          M Offline
                          M Offline
                          MostafaEzzat
                          wrote on last edited by MostafaEzzat
                          #12
                          @mrjj 
                          i did that right now also the button not included and this an image for the QTableView 
                          and for this line :
                                //  model->setData(item, QVariant::fromValue(MyObject.getSpecificInformation()));
                          
                          this line used with QSingleMapper so i don't know which object used with with that 
                          this piece of code contains full code of QsingleMapper way 
                          QSignalMapper *signalMapper = new QSignalMapper(this); [ QSignalMapper *signalMapper = new QSignalMapper(this);] 
                          
                          for( int i=0; i<7; i++ ) { //replace rows.length with your list or vector which consists of the data for your rows.
                               //do something with your data for normal cells...         
                               auto item = model->index(i, 6);
                               model->setData(item, QVariant::fromValue(yourObject.getSpecificInformation()));
                          
                               //make new button for this row 
                               item = model->index(i, 6); 
                               QPushButton *ViewProfile = new QPushButton("ViewProfile");
                              view->setIndexWidget(item, ViewProfile);
                          
                               signalMapper->setMapping(ViewProfile, i);
                               connect(cartButton, SIGNAL(clicked(bool)), signalMapper, SLOT(map()));
                          }
                             connect(signalMapper, SIGNAL(mapped(int)), this, SLOT(on_pushButton_2_clicked()));
                          
                          ![alt text]("https://ibb.co/4FZYK2G")
                          
                          an url of image if didn't appear  : "https://ibb.co/4FZYK2G"
                          
                          1 Reply Last reply
                          0
                          • SGaistS Offline
                            SGaistS Offline
                            SGaist
                            Lifetime Qt Champion
                            wrote on last edited by
                            #13

                            Can you reproduce that situation with a minimal compilable example ?

                            Interested in AI ? www.idiap.ch
                            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                            M 1 Reply Last reply
                            0
                            • SGaistS SGaist

                              Can you reproduce that situation with a minimal compilable example ?

                              M Offline
                              M Offline
                              MostafaEzzat
                              wrote on last edited by MostafaEzzat
                              #14

                              @SGaist

                              this is the full function's code and for sorry the same result without printing the button and i got the code from StackOver Flow from this Answer "https://stackoverflow.com/a/36356873"
                              and maybe this code doesn't work coz of this line : model->setData(item, QVariant::fromValue(yourObject.getSpecificInformation()));

                              that i don't know which object should i replace here "yourObject.getSpecificInformation())" and if there any reliable solution would be better then .. and thank you for your efforts

                              void MainProcess::on_pushButton_2_clicked()
                              {
                                  QSqlDatabase  Databasee =   QSqlDatabase::addDatabase("QMYSQL" , "thirdConnection");
                                  Databasee.setHostName("localhost") ;
                                  Databasee.setUserName("root");
                                  Databasee.setPassword("mostafa");
                                  Databasee.setDatabaseName("Clinic_Database");
                              
                              // Pls Notice that 'QSqlTableModel' doesn't work without making sure that database connected 
                              
                                  if(Databasee.open()){
                                    QMessageBox::about(this,"res","databaseopen") ;
                              
                              
                                  }else {
                                      QMessageBox::about(this,"res","database not open") ;
                                      qDebug() <<  Databasee.lastError();
                                  }
                              
                              
                                  QSqlTableModel *model = new QSqlTableModel(this,  Databasee);
                                  model->setTable("PatientsInfo");
                                  model->setEditStrategy(QSqlTableModel::OnManualSubmit);
                                 model->select();
                                 model->setHeaderData(0, Qt::Horizontal, QObject::tr("PatientsName"));
                                 model->setHeaderData(1, Qt::Horizontal, QObject::tr("Age")) ;
                                 model->setHeaderData(2, Qt::Horizontal, QObject::tr("BloodPrint"));
                                 model->setHeaderData(3, Qt::Horizontal, QObject::tr("PhoneNum"));
                                 model->setHeaderData(4, Qt::Horizontal, QObject::tr("DiagnosisDisease"));
                                 model->setHeaderData(5, Qt::Horizontal, QObject::tr("consultant"));
                                 model->setHeaderData(6, Qt::Horizontal, QObject::tr("ViewProfile"));
                              
                                 QTableView * view = new QTableView;
                                  QSignalMapper *signalMapper = new QSignalMapper(this);
                              
                                 for( int i=0; i<7; i++ ) { //replace rows.length with your list or vector which consists of the data for your rows.
                                      //do something with your data for normal cells...
                                      auto item = model->index(i, 6);
                                   //   model->setData(item, QVariant::fromValue(model->getSpecificInformation()));
                                      //make new button for this row
                                      item = model->index(i, 6);
                                      QPushButton *ViewProfile = new QPushButton("ViewProfile");
                                      view->setIndexWidget(item, ViewProfile);
                                      signalMapper->setMapping(ViewProfile, i);
                                      connect(ViewProfile, SIGNAL(clicked(bool)), signalMapper, SLOT(map()));
                                 }
                                 connect(signalMapper, SIGNAL(mapped(int)), this, SLOT(on_pushButton_2_clicked()));
                              
                                 view->setModel(model);
                                 view->show(); 
                              }
                              
                              1 Reply Last reply
                              0
                              • dheerendraD Offline
                                dheerendraD Offline
                                dheerendra
                                Qt Champions 2022
                                wrote on last edited by dheerendra
                                #15

                                It is issue a with model not associated with view. Before associating model & view, you are trying to get the QModelIndex and setting the widget. You can't do this. index(model->index(i, 6);) what you are obtaining is from model which not associated with View. So move view->setModel before the for loop. Everything should work fine.

                                You can look at the simple example at GIT

                                connect(signalMapper, SIGNAL(mapped(int)), this, SLOT(on_pushButton_2_clicked()));
                                

                                What is the above method. It is calling same method again when you click it. Is that what you wanted to do ?

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

                                M 1 Reply Last reply
                                2
                                • dheerendraD dheerendra

                                  It is issue a with model not associated with view. Before associating model & view, you are trying to get the QModelIndex and setting the widget. You can't do this. index(model->index(i, 6);) what you are obtaining is from model which not associated with View. So move view->setModel before the for loop. Everything should work fine.

                                  You can look at the simple example at GIT

                                  connect(signalMapper, SIGNAL(mapped(int)), this, SLOT(on_pushButton_2_clicked()));
                                  

                                  What is the above method. It is calling same method again when you click it. Is that what you wanted to do ?

                                  M Offline
                                  M Offline
                                  MostafaEzzat
                                  wrote on last edited by
                                  #16

                                  @dheerendra

                                  it works sir thank you so much and i'll add the other parts to ViewProfile of patients .. but thing that i can't understand is why when i close QTableView it close the class itself which i click the button from it to execute the function of the button .. so to prevent that is there any solution except making another Header and Cpp files and execute Constructor in this function "void MainProcess::on_pushButton_2_clicked()
                                  "

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

                                    I did not understand the second part of your question. Can you tell explain more about the question ?

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

                                    M 1 Reply Last reply
                                    0
                                    • dheerendraD dheerendra

                                      I did not understand the second part of your question. Can you tell explain more about the question ?

                                      M Offline
                                      M Offline
                                      MostafaEzzat
                                      wrote on last edited by
                                      #18

                                      @dheerendra

                                      Yes for sure ,
                                      image

                                      here there is a designer form class called MainProcess so there is a Method called void MainProcess::on_pushButton_2_clicked()
                                      if clicked it bringing data from database and preview it in a QTableView so if closed the QTableView and closes the MainProcess Class too so what should i do to make it isolate and don't lose the class
                                      note pls : this class shows after checking login .. and i do appreciate all of your efforts

                                      the url of image : "https://ibb.co/xSp23JN"

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

                                        Looking at you code, it should not close the mainwindow. It should just close the QTableView only. I can see that you are showing only view->show() ; view does not have parent. Just to reconfirm, can you show how are you creating the tableview and showing it ?

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

                                        M 1 Reply Last reply
                                        1
                                        • dheerendraD dheerendra

                                          Looking at you code, it should not close the mainwindow. It should just close the QTableView only. I can see that you are showing only view->show() ; view does not have parent. Just to reconfirm, can you show how are you creating the tableview and showing it ?

                                          M Offline
                                          M Offline
                                          MostafaEzzat
                                          wrote on last edited by MostafaEzzat
                                          #20

                                          @dheerendra

                                          Here in this function if the login successed it will show another window that has process of the programe which have QTableModel as well .. and i initialized a pointer MainProcess * mainprocess .. so it shows the the other Qt Designer form if the login done , and just included QTable and QModelIndex and press the button to get all data from database when click the button in the method which is "void MainProcess::on_pushButton_2_clicked()
                                          can't be sure if it has relation with our method which has the QTableModel
                                          "

                                          void MainWindow::on_pushButton_3_clicked()
                                          {
                                              QSqlDatabase Databas  = QSqlDatabase::database();
                                              /* if(QSqlDatabase::contains("qt_sql_default_connection")){
                                                  Databas = QSqlDatabase::database("qt_sql_default_connection");
                                                qDebug() <<  "1" ;}
                                              else{
                                                  Databas = QSqlDatabase::addDatabase("QMYSQL");
                                                qDebug() <<  "2" ;
                                              }*/
                                              Databas =   QSqlDatabase::addDatabase("QMYSQL", "SecConnection");
                                              Databas.setHostName("localhost") ;
                                              Databas.setUserName("root");
                                              Databas.setPassword("mostafa");
                                              Databas.setDatabaseName("Clinic_Database");
                                              if( QSqlDatabase::contains( "SecConnection" ) )
                                              {
                                                  QSqlDatabase db = QSqlDatabase::database( "SecConnection" );
                                                qDebug() << "Donnne" ;
                                                  //now do some stuff with it
                                              }
                                              else
                                              {
                                                qDebug()  << "something error" ;
                                          
                                                  // connection not found, do something
                                              }
                                              if(Databas.open()){
                                                  qDebug() <<  "Connected" ;
                                              }else {
                                          
                                                 qDebug() << Databas.lastError().text();
                                                qDebug() << "not Connected";
                                              }
                                          
                                              QSqlQuery qr (Databas) ;
                                              qr.exec("select * from Employees") ;
                                              while(qr.next()) {
                                                  qDebug() << qr.value(1) ;
                                          
                                              }
                                          
                                              QSqlQueryModel *model = new QSqlQueryModel();
                                              QSqlQuery *qry = new QSqlQuery(Databas);
                                              QString User = ui->lineEdit_Username_3->text();
                                              QString Pass = ui->lineEdit_2_Password_3->text() ;
                                              if(User==NULL||Pass==NULL) {
                                          
                                                  QMessageBox::information(this,"Result","Pls Fill The User and Password") ;
                                                  return ;
                                          
                                              }
                                                   qry->prepare(" select Employees.Name , Employees.Password from  Employees WHERE Name = :User  AND  Password = :Pass ");
                                                   qry->bindValue(":User", User) ;
                                                   qry->bindValue(":Pass",Pass) ;
                                          
                                                      if(qry->exec())
                                                      {
                                                          if(qry->next())
                                                          {
                                                          model->setQuery(*qry);
                                                          QString  S =  qry->value(0).toString() + "Login Successed ";
                                                          QMessageBox::information(this,"Result",S) ;
                                                          hide();
                                                          mainprocess = new MainProcess(this) ;
                                                          mainprocess->show();
                                                          }
                                                          else
                                                              {
                                                             QMessageBox::information(this,"Result","Sorry Try Again") ;
                                                             qDebug() << qry->lastError();
                                                              }
                                          
                                                        }
                                                                else {qDebug() << qry->lastError();}
                                             }
                                          
                                          
                                          1 Reply Last reply
                                          0

                                          • Login

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