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 3.0k 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.
  • 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
                    • dheerendraD Offline
                      dheerendraD Offline
                      dheerendra
                      Qt Champions 2022
                      wrote on last edited by
                      #21

                      No it is nothing to do with this method. This has no relation to another slots. Have you looked at the example which I have given y'day ?

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

                      M 1 Reply Last reply
                      0
                      • dheerendraD dheerendra

                        No it is nothing to do with this method. This has no relation to another slots. Have you looked at the example which I have given y'day ?

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

                        @dheerendra
                        yes for sure i've checked it and make it as yours (initialized QSqlTableModel and QTableView in the header file)

                        now i create another islolated header and cpp file and put the function i wanna do in them and also same problem that if i closed QTableView it closes the other class , it executes the function but also closes

                        this is the link of files on GitHub : https://github.com/MOSTAFAEZZAT/QTableViewProlem

                        Update : Sir this Problem is solved now it was because of initializing the next window by pointer not as usual object
                        i.e :

                        class MainWindow : public QMainWindow
                        {
                            
                        
                            Q_OBJECT
                        
                        public:
                            explicit MainWindow(QWidget *parent = nullptr);
                            ~MainWindow();
                        
                        private slots:
                            void on_pushButton_3_clicked();
                            void on_pushButton_4_clicked();
                            void on_pushButton_8_clicked();
                        
                        private:
                            Ui::MainWindow * ui;
                            MainProcess    mainprocess ;
                        
                        };
                        
                        

                        this is a piece of function which if the login done show the next Window else no

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

                        so in previous
                        it was like that
                        MainProcess * mainprocess

                        but pls i just wanna know why it was destroying this window(MainProcess) if i closed QTableView .. And Thank you so much in Advance

                        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