Set current portName (QSerialPort)
Hi all!
The examples show code in Terminal project:
Code:
//in constructor
fillPortsInfo();
//..//
void Settings::fillPortsInfo()
{
ui->serialPortInfoListBox->clear();
foreach (const QSerialPortInfo &info, QSerialPortInfo::availablePorts()) {
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.
Re: Set current portName (QSerialPort)
What is the object list needed for?
Re: Set current portName (QSerialPort)
Did you mean something like this?
Code:
void Settings::fillPortsInfo()
{
ui->serialPortInfoListBox->clear();
foreach (const QSerialPortInfo &info, QSerialPortInfo::availablePorts()) {
list << info.portName();
}
ui->serialPortInfoListBox->addItems(list);
}
1 Attachment(s)
Re: Set current portName (QSerialPort)
Quote:
Originally Posted by
Lesiok
What is the object list needed for?
Quote:
Originally Posted by
high_flyer
Did you mean something like this?
I have a list of ports:
Code:
void Settings::fillPortsInfo()
{
ui->serialPortInfoListBox->clear();
foreach (const QSerialPortInfo &info, QSerialPortInfo::availablePorts()) {
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:
Code:
("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:
Attachment 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.
Re: Set current portName (QSerialPort)
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
Re: Set current portName (QSerialPort)
Quote:
Originally Posted by
high_flyer
Thank you! :) It solved my problem.
Code:
void Settings::fillPortsInfo()
{
ui->serialPortInfoListBox->clear();
foreach (const QSerialPortInfo &info, QSerialPortInfo::availablePorts()) {
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());
}