PDA

View Full Version : Set current portName (QSerialPort)



maratk1n
11th April 2017, 18:33
Hi all!
The examples show code in Terminal project:


//in constructor
fillPortsInfo();
//..//
void Settings::fillPortsInfo()
{
ui->serialPortInfoListBox->clear();
foreach (const QSerialPortInfo &info, QSerialPortInfo::availablePorts()) {
QStringList list;
list << info.portName()
<< info.description()
<< info.manufacturer()
<< info.systemLocation()
<< (info.vendorIdentifier() ? QString::number(info.vendorIdentifier(), 16) : QString())
<< (info.productIdentifier() ? QString::number(info.productIdentifier(), 16) : QString());

ui->serialPortInfoListBox->addItem(list.first(), list);
}
}
}

How can I display the current port in the QComboBox (serialPortInfoListBox) that I take from the config.ini file?
Now I have the first (or last) port from the list of ports.

Lesiok
12th April 2017, 07:44
What is the object list needed for?

high_flyer
12th April 2017, 11:03
Did you mean something like this?


void Settings::fillPortsInfo()
{
ui->serialPortInfoListBox->clear();
QStringList list;
foreach (const QSerialPortInfo &info, QSerialPortInfo::availablePorts()) {

list << info.portName();
}
ui->serialPortInfoListBox->addItems(list);
}

maratk1n
12th April 2017, 12:48
What is the object list needed for?


Did you mean something like this?

I have a list of ports:


void Settings::fillPortsInfo()
{
ui->serialPortInfoListBox->clear();
foreach (const QSerialPortInfo &info, QSerialPortInfo::availablePorts()) {
QStringList list;
list << info.portName()
<< info.description()
<< info.manufacturer()
<< info.systemLocation()
<< (info.vendorIdentifier() ? QString::number(info.vendorIdentifier(), 16) : QString())
<< (info.productIdentifier() ? QString::number(info.productIdentifier(), 16) : QString());
qDebug() << list;
ui->serialPortInfoListBox->addItem(list.first(), list);
}
}

qDebug output:


("COM16", "HUAWEI Mobile Connect - 3G PC UI Interface", "HUAWEI Incorporated", "\\.\COM16", "12d1", "1003")
("COM1", "MOXA Port 0", "Moxa Inc.", "\\.\COM1", "", "")
("COM2", "MOXA Port 1", "Moxa Inc.", "\\.\COM2", "", "")
("COM17", "HUAWEI Mobile Connect - 3G Modem", "HUAWEI Incorporated", "\\.\COM17", "12d1", "1003")

The application is configured to the COM2 (I take it from the config file).
The user opens the port settings window and sees it there:
12440
I do not want the user to panic and think that another port is connected. Therefore, I initially want to set the current port.
But I can not think of how to do it.

high_flyer
12th April 2017, 13:41
If the comobo box is not editable, your can simply call setCurrentText() with the port name on it.
http://doc.qt.io/qt-5/qcombobox.html#currentText-prop

maratk1n
12th April 2017, 16:25
If the comobo box is not editable, your can simply call setCurrentText() with the port name on it.
http://doc.qt.io/qt-5/qcombobox.html#currentText-prop

Thank you! :) It solved my problem.



void Settings::fillPortsInfo()
{
ui->serialPortInfoListBox->clear();
foreach (const QSerialPortInfo &info, QSerialPortInfo::availablePorts()) {
QStringList list;
list << info.portName()
<< info.description()
<< info.manufacturer()
<< info.systemLocation()
<< (info.vendorIdentifier() ? QString::number(info.vendorIdentifier(), 16) : QString())
<< (info.productIdentifier() ? QString::number(info.productIdentifier(), 16) : QString());
//qDebug() << list;
ui->serialPortInfoListBox->addItem(list.first(), list);
}
ui->serialPortInfoListBox->setCurrentText(Config::get().getValue("COM/name").toString());
}