Combo box - Serial [SOLVED]



  • 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?


  • 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



  • 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]


  • Lifetime Qt Champion

    Hi,

    What error are you getting ?



  • @#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?


  • Lifetime Qt Champion

    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 ?



  • Can you mail me? :)


  • Lifetime Qt Champion

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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.