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. [Solved]Combobox index using as input for function
Forum Updated to NodeBB v4.3 + New Features

[Solved]Combobox index using as input for function

Scheduled Pinned Locked Moved General and Desktop
10 Posts 3 Posters 2.8k Views 1 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.
  • B Offline
    B Offline
    BobMarly
    wrote on last edited by
    #1

    I have different files and i want based on the index/name of the combobox that the file i use changes. i have something like this now, but it does not work:
    @MWidget::MWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::MWidget)
    {
    ui->setupUi(this);
    connect(ui->comboBox,SIGNAL(currentIndexChanged(int)),this,SLOT(on_comboBox_currentIndexChanged(int)));

    QStringList List;
    List<<"C:\\Airfoils\\a18.dat"<<"C:\\Airfoils\\a18sm.dat"<<"C:\\Airfoils\\a63a108c.dat"<<"C:/Airfoils/ag03.dat";
    
    //read file for drawing
    QFile file&#40;List[index]&#41;;
    

    void MWidget::on_comboBox_currentIndexChanged(int index)
    {
    switch (index) {
    case 0 :
    ui->comboBox->currentIndexChanged(0);
    break;
    case 1 :
    ui->comboBox->currentIndexChanged(1);
    break;
    }@
    I cut out irrelevant code parts, since without the input form the combobox the loading from files works, based on the order of which they are in the list. I also want to do this for about 1500 files..

    1 Reply Last reply
    0
    • JeroentjehomeJ Offline
      JeroentjehomeJ Offline
      Jeroentjehome
      wrote on last edited by
      #2

      Hi,
      Your ui->combobox->currentIndexChanged is a SIGNAL! Not a slot, you it can be compiled, but not called! Why don't you connect both comboboxes to each other?
      @
      connect(ui->comboBox1, SIGNAL(currentIndexChanged(in),
      ui->comboBox2, SLOT(setCurrentIndex(int)) );
      @
      Or something like that?

      Greetz, Jeroen

      1 Reply Last reply
      0
      • B Offline
        B Offline
        BobMarly
        wrote on last edited by
        #3

        I only want one combobox..just a combobox with a lot of options, and for each option it should call the file belonging to that, so it will draw something with the coordinates of that file.. (nieuw @ Qt)

        1 Reply Last reply
        0
        • JeroentjehomeJ Offline
          JeroentjehomeJ Offline
          Jeroentjehome
          wrote on last edited by
          #4

          Hmm,
          Oke,
          Use the slot currentIndexChanged(), and connect to a slot. In that slot you are able to do anything you like. Like reading files, updating GUI elements etc etc,
          Ben jij NL?
          Welkom trouwens!

          Greetz, Jeroen

          1 Reply Last reply
          0
          • JeroentjehomeJ Offline
            JeroentjehomeJ Offline
            Jeroentjehome
            wrote on last edited by
            #5

            Also, don't you want just a single QFile and a QStringList to hold the file names? Then you could generate a slot that will update the QFile and sends a signal to "read that file" and update on screen. Since your a noob, let's give some code:
            @
            // hpp file:
            class ...
            private:
            QFile* m_File_p;
            QStringList m_MyFileList;
            ...
            }
            //cpp file
            MWidget::MWidget(QWidget* parent) : QWidget(parent), ui(new Ui::MWidget)
            {
            // File your m_MyFileList;

            ReadFile&#40;MyFileList.at(0&#41;);
            

            connect(ui->comboBox, SIGNAL(currentIndexChanged(int)),
            this, SLOT(ChangeFile(int));
            }

            void MWidget::ChangeFile(int NewIndex_i)
            {
            if (NewIndex_i < MyFileList.count())
            {
            // New file to read, may also be done via signal/slots!
            ReadFile(MyFileList.at(NewIndex));
            }
            }

            void MWidget::ReadFile(QString NewFileName)
            {
            m_File_p = new QFile (NewFileName);
            if (m_File_p != NULL)
            {
            if ( (m_File_p.exists() == true) &&
            (m_File_p.open(QIODEVICE::ReadWrite) == true) )
            {
            // Do your read file stuff
            }
            m_File_p.flush();
            m_File_p.close();
            // Release memory again.
            delete m_File_p;
            }
            }
            @

            Greetz, Jeroen

            1 Reply Last reply
            0
            • B Offline
              B Offline
              BobMarly
              wrote on last edited by
              #6

              Dank je!. I tried implementing your code into mine but it does not work..
              @MWidget::MWidget(QWidget *parent) :
              QWidget(parent),
              ui(new Ui::MWidget)
              {
              ui->setupUi(this);
              ReadFile(MyFileList.at(0));
              connect(ui->comboBox,SIGNAL(currentIndexChanged(int)),this,SLOT(ChangeFile(int)));
              }

              void MWidget::ChangeFile(int NewIndex)
              { MyFileList<<"C:\Airfoils\a18.dat"<<"C:\Airfoils\a18sm.dat"<<"C:\Airfoils\a63a108c.dat"<<"C:/Airfoils/ag03.dat";
              if(NewIndex<MyFileList.count())
              {
              ReadFile(MyFileList.at(NewIndex));
              }
              }

              void MWidget::ReadFile(QString NewFileName)
              {QFile m_File_p(NewFileName);

              if (!m_File_p->open(QIODevice::ReadOnly | QIODevice::Text))
                  return ;
              QTextStream in(&m_File_p);
              QString line = in.readLine();
              bool blnFirstLine =true;
              float flScale = 640;
              while (!line.isNull())
              {
              
                  if(blnFirstLine)
                  {
                      blnFirstLine=false;
                  }else{
              
                      QStringList coordinates = line.split(",", QString::SkipEmptyParts );
              
                      if( coordinates.count() == 2 )
                      {
                          bool checkX;
                          float x = coordinates.at(0).trimmed().toFloat( &checkX )*flScale+100;
                          bool checkY;
                          float y = coordinates.at(1).trimmed().toFloat( &checkY )*(-1)*flScale+100;
              
                          if( checkX && checkY )
                              polyPoints  << QPointF(x,y);
                      }
                  }
              
              
                  line = in.readLine();
              }
              m_File_p.flush();
              m_File_p.close();
              delete m_File_p;
              

              }

              //![6]
              @

              1 Reply Last reply
              0
              • B Offline
                B Offline
                BobMarly
                wrote on last edited by
                #7

                It gives an error message :"QList index out of range" and then closes..also the delete function gives errors..

                1 Reply Last reply
                0
                • N Offline
                  N Offline
                  NicuPopescu
                  wrote on last edited by
                  #8

                  bq. Also where do i load my files now as i did before in stringlist?

                  as far as I see, in MWidget's constructor and MyFileList must be a member of it ... but you could keep the file names directly in combo box as data by using:

                  @ comboBox->setItemData(0,QVariant(filename),Qt::UserRole);@

                  1 Reply Last reply
                  0
                  • B Offline
                    B Offline
                    BobMarly
                    wrote on last edited by
                    #9

                    The program is working!! Only still need to remove the previous drawn item(which would probably work if the delete works i guess??)

                    1 Reply Last reply
                    0
                    • B Offline
                      B Offline
                      BobMarly
                      wrote on last edited by
                      #10

                      How do i remove the previously drawn item( that was drawn via selection in the combobox) when i select a new one? now all images are written on top of each other because it remembers all previous selections..

                      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