[Interest] Update QComboBox items on opening the combo box

André Hartmann andre.hartmann at iseg-hv.de
Mon Jun 30 11:36:57 CEST 2014


Hello all,

I'd like to have a QComboBox with the available serial ports on my 
system. Populating the box at program start is easy, but I'd like to 
detect USB serial ports that are connected during run time.

The only think I found is focusInEvent, but it seems my port list is 
updated twice, on opening the list and on closing it (Windows 7, Qt 
4.8.2). Is there a better way to do?

I'd really like to have something like QComboBox::onOpened().
Borland C++ Builder had OnDropDown which for that purpose.

Thanks in advance.

#include "serialportcombobox.h"

#include <QtSerialPort/QSerialPortInfo>

SerialPortComboBox::SerialPortComboBox(QWidget *parent) :
   QComboBox(parent)
{
   updatePortList();
}

void SerialPortComboBox::focusInEvent(QFocusEvent *event)
{
   updatePortList();

   QComboBox::focusInEvent(event);
}

void SerialPortComboBox::updatePortList()
{
   clear();

   foreach (const QSerialPortInfo &info,
            QSerialPortInfo::availablePorts()) {
	     addItem(info.portName());
   }
}

#ifndef SERIALPORTCOMBOBOX_H
#define SERIALPORTCOMBOBOX_H

#include <QComboBox>

class SerialPortComboBox : public QComboBox
{
	Q_OBJECT
public:
	explicit SerialPortComboBox(QWidget *parent = 0);

protected:
	virtual void focusInEvent(QFocusEvent *event);

private:
	void updatePortList();

signals:

public slots:
};

#endif // SERIALPORTCOMBOBOX_H

-- 
Best regards / Mit freundlichen Grüßen
André Hartmann, Dipl.-Ing. (FH)
Software Project Manager

iseg Spezialelektronik GmbH         |  phone: ++49 (0)351 26996-43
Bautzner Landstr. 23                |  fax:   ++49 (0)351 26996-21
D-01454 Radeberg / Rossendorf       |  web:   www.iseg-hv.com

Geschäftsführer / Managing director: Dr. F. Gleisberg, Dr. J. Pöthig
Amtsgericht / Lower district court: Dresden HRB 16250
Ust.-Id.-Nr. / VAT-ID: DE812508942

Diese E-Mail enthält vertrauliche und/oder rechtlich geschützte
Informationen. Wenn Sie nicht der richtige Adressat sind oder
diese E-Mail irrtümlich erhalten haben, informieren Sie bitte
sofort den Absender und vernichten Sie diese Mail.
Das unerlaubte Kopieren sowie die unbefugte Weitergabe dieser
Mail ist nicht gestattet.

This e-mail may contain confidential and/or privileged information.
If you are not the intended recipient (or have received this e-mail
in error) please notify the sender immediately and delete this e-mail.
Any unauthorized copying, disclosure or distribution of the material
in this e-mail is strictly forbidden.



More information about the Interest mailing list