Combo box - Serial [SOLVED]
-
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]
-
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?
-
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 ?
-
You should not post your email address here, it's not protected.