[Solved]Combobox index using as input for function



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



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



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



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



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



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



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



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



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



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


Log in to reply
 

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