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. Combo box - Serial [SOLVED]
QtWS25 Last Chance

Combo box - Serial [SOLVED]

Scheduled Pinned Locked Moved General and Desktop
8 Posts 3 Posters 5.1k Views
  • 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.
  • C Offline
    C Offline
    c77asper
    wrote on 2 Jun 2014, 11:22 last edited by
    #1

    Hey :)
    I try to make a combo box that can show me the available ports on my computer?

    foreach(const QSerialPortInfo &info, QSerialPortInfo::availablePorts() )
    {
        ui->Com_port->addItem(info.availablePorts());    <--- I think this i wrong?
    
    1 Reply Last reply
    0
    • J Offline
      J Offline
      Jeroentjehome
      wrote on 2 Jun 2014, 11:37 last edited by
      #2

      Hi,
      Yup, You need to utilize the
      @
      QSerialPortInfo::portName();
      @
      That will return a QString
      So someting like this:
      @
      foreach (const QSerialPortInfo &ThisPort, QSerialPortInfo::availablePorts())
      {
      ui->comboBoxComPorts->addItem(ThisPort.portName());
      }
      @
      The addItem of the comboBox may take a QString, not a QSerialPortInfo class

      Greetz, Jeroen

      1 Reply Last reply
      0
      • C Offline
        C Offline
        c77asper
        wrote on 2 Jun 2014, 12:19 last edited by
        #3

        Thanks!
        I still got an error :)
        this is my code:

        @
        #include "mainwindow.h"
        #include "ui_mainwindow.h"
        #include <QtSerialPort/QSerialPort>
        #include <QSerialPortInfo>
        #include <QDebug>
        #include <QSettings>
        #include <QMessageBox>

        QSerialPort *serial;
        QSerialPortInfo::portName();

        MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
        {
        ui->setupUi(this);

        serial = new QSerialPort(this);
        serial->setPortName("com12");
        serial->setBaudRate(QSerialPort::Baud9600);
        serial->setDataBits(QSerialPort::Data8);
        serial->setParity(QSerialPort::NoParity);
        serial->setStopBits(QSerialPort::OneStop);
        serial->setFlowControl(QSerialPort::NoFlowControl);
        serial->open(QIODevice::ReadWrite);
        serial->write("Hello");
        connect(serial,SIGNAL(readyRead()),this,SLOT(SerialRecieve()));
        

        }

        void MainWindow::SerialRecieve()
        {

        QByteArray Recieved_data;
        Recieved_data=serial->readAll();
        ui->label->setText(Recieved_data);
        qDebug()<<Recieved_data;
        }

        MainWindow::~MainWindow()
        {
        delete ui;
        serial->close();
        }

        void MainWindow::SetComPort()
        {

        foreach (const QSerialPortInfo &Port, QSerialPortInfo::availablePorts() )
        {
            ui->Com_port->addItem(Port.portName());
        }
        

        }
        @

        [edit: Added missing coding tags @ SGaist]

        1 Reply Last reply
        1
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 2 Jun 2014, 21:38 last edited by
          #4

          Hi,

          What error are you getting ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • C Offline
            C Offline
            c77asper
            wrote on 3 Jun 2014, 05:40 last edited by
            #5

            @#include "mainwindow.h"
            #include "ui_mainwindow.h"
            #include <QtSerialPort/QSerialPort>
            #include <QSerialPortInfo>
            #include <QDebug>
            #include <QSettings>
            #include <QMessageBox>

            QSerialPort *serial;

            MainWindow::MainWindow(QWidget *parent) :
            QMainWindow(parent),
            ui(new Ui::MainWindow)
            {
            ui->setupUi(this);

            // serial = new QSerialPort(this);
            // serial->setPortName("com12");
            // serial->setBaudRate(QSerialPort::Baud9600);
            // serial->setDataBits(QSerialPort::Data8);
            // serial->setParity(QSerialPort::NoParity);
            // serial->setStopBits(QSerialPort::OneStop);
            // serial->setFlowControl(QSerialPort::NoFlowControl);
            // serial->open(QIODevice::ReadWrite);
            // serial->write("Hello");

            }

            MainWindow::~MainWindow()
            {
            delete ui;
            serial->close();
            }

            void MainWindow::SetComPort()
            {

            foreach (const QSerialPortInfo &Port, QSerialPortInfo::availablePorts() )
            {
                ui->Com_port->addItem(Port.portName());
            }
            

            }@

            This is my code now - i don't get an error right now, but in my combo box, i don't see any com ports?

            1 Reply Last reply
            0
            • S Offline
              S Offline
              SGaist
              Lifetime Qt Champion
              wrote on 3 Jun 2014, 07:46 last edited by
              #6

              Because you never call SetComPort. Which is not really a descriptive name for what the method does.

              Also why are you using a static QSerialPort ?

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • C Offline
                C Offline
                c77asper
                wrote on 3 Jun 2014, 09:00 last edited by
                #7

                Can you mail me? :)

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on 3 Jun 2014, 20:38 last edited by
                  #8

                  You should not post your email address here, it's not protected.

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  0

                  7/8

                  3 Jun 2014, 09:00

                  • Login

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