Important: Please read the 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
            // If the file cannot be opened for reading...
            if (! | QIODevice::Text))
               //...display a warning dialogue
               QMessageBox::warning(this, tr("Qt Directory"),
                            tr("This file cannot be opened for reading."),
                            QMessageBox::Cancel, QMessageBox::Cancel);
                // Otherwise, create a text file
                QTextStream in(&dataFile);
                // Initialise the count of values in the file
                //While not at the end of the file
                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);
                     //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();
                    // No person object created
               // do work...
            // Close file
            // Print file name with extension.
            QFileInfo fi(fileName);    
            QString name = fi.fileName();  // base = "archive"
            this->setWindowTitle("Directory - " + name);
            // Update form

    Your help would be greatly appreciated. Thank you.

  • Lifetime Qt Champion


    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