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. How to update QComboBox?
Forum Updated to NodeBB v4.3 + New Features

How to update QComboBox?

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 3 Posters 976 Views 2 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.
  • kahlenbergK Offline
    kahlenbergK Offline
    kahlenberg
    wrote on last edited by
    #1

    Hi,
    I have following typedef with name FT_DEVICE_LIST_INFO_NODE:

    	typedef struct _ft_device_list_info_node {
    		ULONG Flags;
    		ULONG Type;
    		ULONG ID;
    		DWORD LocId;
    		char SerialNumber[16];
    		char Description[64];
    		FT_HANDLE ftHandle;
    	} FT_DEVICE_LIST_INFO_NODE;
    

    I have a vector of this type and this vector is updated every 500ms:

    std::vector<FT_DEVICE_LIST_INFO_NODE> list
    

    I want to update every 500 ms a QComboBox on GUI. But first I have to check whether or not combobox contains the same values.

    The following code is called every 500ms but it doesn't work as expected. It adds an element more than one every 500ms. Combobox is getting bigger and bigger with same values:

    void MainWindow::on_UpdateDeviceInfos(std::vector<FT_DEVICE_LIST_INFO_NODE> list)
    {
    
        int nrOfDevices = list.size();
        int nrOfcomboBoxItems = ui->comboBoxDevices->count();
    
        QVector<DWORD> locIDs;
    
        for (int index = 0; index < nrOfcomboBoxItems; index++)
        {
            locIDs.append(ui->comboBoxDevices->itemText(index).mid(22).toInt());
        }
    
        if(locIDs.empty())
            ui->comboBoxDevices->addItem(QString("Device %1 (%2) - %3").arg(0).arg(QString(list[0].Description)).arg(list[0].LocId));
    
        foreach( DWORD locID , locIDs)
        {
            auto it = std::find_if(list.begin(), list.end(), [locID](const FT_DEVICE_LIST_INFO_NODE & item) { return item.LocId == locID; });
            if (it != end(list))
                ui->comboBoxDevices->addItem(QString("Device %1 (%2) - %3").arg(0).arg(QString(it->Description)).arg(it->LocId));
        }
    }
    
    

    How can I make it "correctly"?

    1 Reply Last reply
    0
    • M Offline
      M Offline
      Mr Gisa
      wrote on last edited by Mr Gisa
      #2

      You can make use of QTimer for that. Just make usage of signals and slots and you are good to go.

      QTimer *timer = new QTimer(this);
      connect(timer, SIGNAL(timeout()), this, SLOT(update()));
      timer->start(1000);
      

      From then on, the update() slot is called every second.

      https://doc.qt.io/qt-5/qtimer.html#details

      1 Reply Last reply
      0
      • FlotisableF Offline
        FlotisableF Offline
        Flotisable
        wrote on last edited by Flotisable
        #3

        It seems that you want to add the item in the list to the comboBoxDevice if there is no such item.

        But what you did is to find is there an item in the list has the same locid as the item in the comboBoxDevice,
        and if it is, you add the item which has the same locid to the comboBoxDevice.

        I think the foreach should be written like this

        foreach( const FT_DEVICE_LIST_INFO_NODE &node, list )
        {
            auto it = std::find_if( locIDs.begin(), locIDs.end(), [&]( const DWORD &item ) { return item == node.LocId; } );
            if ( it == end( locIDs ) )
                ui->comboBoxDevices->addItem(QString("Device %1 (%2) - %3").arg(0).arg(QString(node->Description)).arg(node->LocId));
        }
        
        1 Reply Last reply
        3

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved