Results 1 to 6 of 6

Thread: Set current portName (QSerialPort)

  1. #1
    Join Date
    Apr 2017
    Posts
    30
    Thanks
    9
    Qt products
    Qt5
    Platforms
    Windows

    Default Set current portName (QSerialPort)

    Hi all!
    The examples show code in Terminal project:
    Qt Code:
    1. //in constructor
    2. fillPortsInfo();
    3. //..//
    4. void Settings::fillPortsInfo()
    5. {
    6. ui->serialPortInfoListBox->clear();
    7. foreach (const QSerialPortInfo &info, QSerialPortInfo::availablePorts()) {
    8. list << info.portName()
    9. << info.description()
    10. << info.manufacturer()
    11. << info.systemLocation()
    12. << (info.vendorIdentifier() ? QString::number(info.vendorIdentifier(), 16) : QString())
    13. << (info.productIdentifier() ? QString::number(info.productIdentifier(), 16) : QString());
    14.  
    15. ui->serialPortInfoListBox->addItem(list.first(), list);
    16. }
    17. }
    18. }
    To copy to clipboard, switch view to plain text mode 
    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.

  2. #2
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Set current portName (QSerialPort)

    What is the object list needed for?

  3. #3
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Set current portName (QSerialPort)

    Did you mean something like this?
    Qt Code:
    1. void Settings::fillPortsInfo()
    2. {
    3. ui->serialPortInfoListBox->clear();
    4. foreach (const QSerialPortInfo &info, QSerialPortInfo::availablePorts()) {
    5.  
    6. list << info.portName();
    7. }
    8. ui->serialPortInfoListBox->addItems(list);
    9. }
    To copy to clipboard, switch view to plain text mode 
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  4. #4
    Join Date
    Apr 2017
    Posts
    30
    Thanks
    9
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Set current portName (QSerialPort)

    Quote Originally Posted by Lesiok View Post
    What is the object list needed for?
    Quote Originally Posted by high_flyer View Post
    Did you mean something like this?
    I have a list of ports:
    Qt Code:
    1. void Settings::fillPortsInfo()
    2. {
    3. ui->serialPortInfoListBox->clear();
    4. foreach (const QSerialPortInfo &info, QSerialPortInfo::availablePorts()) {
    5. list << info.portName()
    6. << info.description()
    7. << info.manufacturer()
    8. << info.systemLocation()
    9. << (info.vendorIdentifier() ? QString::number(info.vendorIdentifier(), 16) : QString())
    10. << (info.productIdentifier() ? QString::number(info.productIdentifier(), 16) : QString());
    11. qDebug() << list;
    12. ui->serialPortInfoListBox->addItem(list.first(), list);
    13. }
    14. }
    To copy to clipboard, switch view to plain text mode 
    qDebug output:
    Qt Code:
    1. ("COM16", "HUAWEI Mobile Connect - 3G PC UI Interface", "HUAWEI Incorporated", "\\.\COM16", "12d1", "1003")
    2. ("COM1", "MOXA Port 0", "Moxa Inc.", "\\.\COM1", "", "")
    3. ("COM2", "MOXA Port 1", "Moxa Inc.", "\\.\COM2", "", "")
    4. ("COM17", "HUAWEI Mobile Connect - 3G Modem", "HUAWEI Incorporated", "\\.\COM17", "12d1", "1003")
    To copy to clipboard, switch view to plain text mode 
    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:
    image.jpg
    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.
    Last edited by maratk1n; 12th April 2017 at 12:06.

  5. #5
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default 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
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  6. The following user says thank you to high_flyer for this useful post:

    maratk1n (12th April 2017)

  7. #6
    Join Date
    Apr 2017
    Posts
    30
    Thanks
    9
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Set current portName (QSerialPort)

    Quote Originally Posted by high_flyer View Post
    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.

    Qt Code:
    1. void Settings::fillPortsInfo()
    2. {
    3. ui->serialPortInfoListBox->clear();
    4. foreach (const QSerialPortInfo &info, QSerialPortInfo::availablePorts()) {
    5. list << info.portName()
    6. << info.description()
    7. << info.manufacturer()
    8. << info.systemLocation()
    9. << (info.vendorIdentifier() ? QString::number(info.vendorIdentifier(), 16) : QString())
    10. << (info.productIdentifier() ? QString::number(info.productIdentifier(), 16) : QString());
    11. //qDebug() << list;
    12. ui->serialPortInfoListBox->addItem(list.first(), list);
    13. }
    14. ui->serialPortInfoListBox->setCurrentText(Config::get().getValue("COM/name").toString());
    15. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by maratk1n; 12th April 2017 at 15:34.

Similar Threads

  1. QSerialPort
    By fmarques in forum Qt Programming
    Replies: 0
    Last Post: 20th April 2016, 16:04
  2. QserialPort
    By arturs in forum Newbie
    Replies: 0
    Last Post: 13th May 2015, 20:37
  3. Qserialport issue in QT4
    By coss_cat in forum Qt Programming
    Replies: 3
    Last Post: 11th December 2013, 18:11
  4. QSerialport in multithread
    By snow_starzz in forum Newbie
    Replies: 3
    Last Post: 3rd December 2013, 10:18
  5. Qt5 cmake and QSerialPort
    By Chris.Burner in forum Newbie
    Replies: 1
    Last Post: 21st April 2013, 16:13

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.