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. Connecting QMainwindow Button to if-statement
Forum Updated to NodeBB v4.3 + New Features

Connecting QMainwindow Button to if-statement

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 3 Posters 318 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.
  • J Offline
    J Offline
    jefazo92
    wrote on last edited by
    #1

    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.

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

      Hi,

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

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

      1 Reply Last reply
      0
      • J jefazo92

        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.

        Pl45m4P Offline
        Pl45m4P Offline
        Pl45m4
        wrote on last edited by Pl45m4
        #3

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


        If debugging is the process of removing software bugs, then programming must be the process of putting them in.

        ~E. W. Dijkstra

        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