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



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

  • Lifetime Qt Champion

    Hi,

    What exactly doesn't work ?



  • @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



  • @SGaist

    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"


  • Lifetime Qt Champion

    So you are talking about the ViewProfile button ?



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



  • @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



  • @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


  • Lifetime Qt Champion

    Something is really not clear here.

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



  • @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 ?


  • Lifetime Qt Champion

    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 ?



  • @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"
    

  • Lifetime Qt Champion

    Can you reproduce that situation with a minimal compilable example ?



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

  • Qt Champions 2017

    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

    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()
    "


  • Qt Champions 2017

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



  • @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"


  • Qt Champions 2017

    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

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

  • Qt Champions 2017

    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
    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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.