Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Connecting QMainwindow Button to if-statement



  • Hi everyone, I am not sure how to put this so I'll try my best. I'm trying to develop a program which will allow the user to store data in an array in four different ways (the index is calculated in a different way depending on the user's choice). I have the indices functions and the fileopen functions coded but I need to make it so that regardless of the option chosen the user will access my fileOpen function and then depending on the index method chosen one of my four index functions will be called and the program will run it. This is what I've got at the moment (Non-Qt program):

    void MainWindow::fileOpen() /*REGARDLESS of selected index option all signals must be linked to this function.*/ 
    {
       QString fileName; //File name entered by user
       QFile dataFile;   //File selected by user
       QStringList list;
       QString line;
    
    
    
         // Create a file open dialogue and return the file name entered
         fileName = QFileDialog::getOpenFileName(this,
         tr("Open File"), ".", tr("All Files (*.csv)"));
         // If the file name is not empty...
         if (!fileName.isEmpty())
           {
    
            // Set the file name
            dataFile.setFileName(fileName);
    
            // If the file cannot be opened for reading...
            if (!dataFile.open(QIODevice::ReadOnly | QIODevice::Text))
              {
               //...display a warning dialogue
               QMessageBox::warning(this, tr("Qt Directory"),
                            tr("This file cannot be opened for reading."),
                            QMessageBox::Cancel, QMessageBox::Cancel);
              }
    
            else                         
              {
                // Otherwise, create a text file
                QTextStream in(&dataFile);
                // Initialise the count of values in the file
    
                //While not at the end of the file
                while(!in.atEnd()){
    
    
                line = in.readLine();   //How does the program know to go onto the next line. */
                list = line.split(',');
    
       Person *tempPerson = new Person(list[1], list[2], list[3], list[4], list[5]);
    
                if (tempPerson)
                {
                    bool check;
    
                    int hashkey = list[0].toInt(&check, 10);
    
                    if(!check)
                    {
                     //Write a cout statement stating what has happened.
                     hashkey = list[0].toInt(&check, 10); //Repeat conversion again.
                    }
    
    /*NOW HERE I would like the program to know depending on the option selected to enter one of 4 if-statements and go into its appropriate function*/
    
                    QString name = tempPerson->getFirstname();
    
                     /* Insert into hash table, using key = list[0](ID), value = tempPerson*/
    
    
                    QString surname = tempPerson->getLastname();
    
                    QString place = tempPerson->getCountry();
    
                    QString mail = tempPerson->getEmail();
                }
                else
                {
                    // No person object created
                }
                //list[0]
    
    
               // do work...
               }
            // Close file
            dataFile.close();
            // Print file name with extension.
            QFileInfo fi(fileName);    
            QString name = fi.fileName();  // base = "archive"
            this->setWindowTitle("Directory - " + name);
            // Update form
            update(); 
        }
      }
    }
    

    Your help would be greatly appreciated. Thank you.


  • Lifetime Qt Champion

    Hi,

    Do you mean that you want the clicked signal of a QPushButton to be connected to the fileOpen method ?



  • @jefazo92

    Is fileOpen() a slot function?

    Where the user selects between the 4 options?
    If you have a dialog, you can check which button was pressed or which checkBox is checked or radio button was toggled...

    I would go with a variable (maybe enum type) to store the selected "indexing mode".
    Inside your fileOpen you can use a switch-case to differentiate between your four options.


Log in to reply