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

How can I add item in the QCombobox?



  • Hi All,
    I'm using qt5.5 and Linux+QT for my OS system.
    This is how I designed.

    void MainWindow::openscanwindow()
    {
        int i = 0;
        bool empty = false;
        QString scan_info;
        QString info;
        stackedWidgetTestbtselect=ui->stackedWidgetPagePanel;
        ui->stackedWidget->setCurrentWidget(stackedWidgetTestbtselect);
        btselect = new QComboBox(this);
        btselect->setEditable(true);
        Process::executeProcessSync(QString("sh"),QStringList()<<"-c"<<"/bin/touch /home/bt.txt",&info);
        qDebug()<<"touch: "+info;
        Process::executeProcessSync(QString("sh"),QStringList()<<"-c"<<"/usr/bin/hciconfig hci0 piscan",&info);
        qDebug()<<"piscan: "+info;
        Process::executeProcessSync(QString("sh"),QStringList()<<"-c"<<"/usr/bin/hcitool scan > /home/bt.txt",&info);
        qDebug()<<"scan: "+info;
        QString jump;
        QFile scan("/home/bt.txt");
        if(!scan.open(QIODevice::ReadOnly|QIODevice::Text))
        {
            qDebug("cannot open bt.txt");
        }
        else
        {
            jump = scan.readLine();
            while(!empty)
            {
                scan_info=scan.readLine();
                qDebug()<<scan_info;
                if(scan_info.isEmpty())
                {
                    empty = true;
                }
                else
                {
                    scan_info = scan_info.mid(19).trimmed();
                    qDebug()<<scan_info;
                    btselect->addItem(scan_info);
                }
            }
    
        }
    
    
    }
    

    I can't show anything out in my Combobox.

    How can I solve this problem?

    Thanks in Advanced!



  • @victor-wang said in How can I add item in the QCombobox?:

            else
            {
                scan_info = scan_info.mid(19).trimmed();
                qDebug()<<scan_info;
                btselect->addItem(scan_info);
            }
    

    Does it print anything?



  • @Taz742
    Yes it will print what I want!



  • @Taz742
    There is a strange thing happened that I can print the log out but I can't show anything in my combobox.



  • @victor-wang

    void MainWindow::openscanwindow()
    {
        bselect = new QComboBox(this);
        bselect->addItem("New Item");
    }
    

    Could it be just a test to work in this case?



  • @Taz742 said in How can I add item in the QCombobox?:

    void MainWindow::openscanwindow()
    {
    bselect = new QComboBox(this);
    bselect->addItem("New Item");
    }

    Could it be just a test to work in this case?

    and bselect.show();



  • @Taz742
    I seem to solve the problem.
    I had make a silly mistake.
    I should do like this.

    else
            {
                scan_info = scan_info.mid(19).trimmed();
                qDebug()<<scan_info;
                ui->combobox->addItem(scan_info);
            }
    


  • @victor-wang
    Yes.
    The more detailed problem was the following

        btselect = new QComboBox(this);
    
    

    You may have been using it for some time but btselect = new QComboBox (this); He spoiled everything.
    Combobox cleaned. He's the other and no longer watches the previous address.
    If you use it only in the constructor then everything would be fine :))



  • @victor-wang
    sorry for english.
    Hopefully there will be someone who is godly and you will explain in detail.


Log in to reply