PDA

View Full Version : QCombobox Click Signal/Event



P51D
30th November 2016, 08:01
Hi every one

I've a question to qcombobox. In my application I would add all current Serial ports to the Combo box during the runtime. I tried to update the list, when the user clicks on the Combobox (read only).

After some searching I found something:
http://www.qtcentre.org/threads/946-send-signal-from-QCombobox
But the problem is, that the Event function is never called... so where is my fault?



#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QQueue>
#include <QLineSeries>
#include <QSerialPortInfo>

namespace Ui {
class MainWindow;
}


class MainWindow : public QMainWindow
{

public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();

private:
Ui::MainWindow *ui;

public slots:
void vUpdatePortList();

protected:
bool bEventFilter(QObject* o, QEvent* e);
};

#endif // MAINWINDOW_H





#include "mainwindow.h"

#include <QLabel>
#include <QTimer>
#include <QChartView>
#include <QValueAxis>
#include <QLineSeries>
#include <QMessageBox>
#include <QSortFilterProxyModel>


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

const QList<QSerialPortInfo> portList(QSerialPortInfo::availablePorts());
int i = 0;
foreach (QSerialPortInfo port, portList)
{
ui->comboBoxPorts->addItem(port.portName());
ui->comboBoxPorts->setItemData(i, port.description(), Qt::ToolTipRole);
i++;
}
// Sort List
QSortFilterProxyModel* proxy = new QSortFilterProxyModel(ui->comboBoxPorts);
proxy->setSourceModel(ui->comboBoxPorts->model());
ui->comboBoxPorts->model()->setParent(proxy);
ui->comboBoxPorts->model()->sort(0);


ui->comboBoxPorts->installEventFilter(this);
}

MainWindow::~MainWindow()
{
delete ui;
}

bool MainWindow::bEventFilter(QObject* o, QEvent* e)
{
if(e->type() == QEvent::Show)
{
vUpdatePortList();
ui->textEdit->append("TRUE");
return true;
}
else
{
ui->textEdit->append("FALSE");
return false;
}
}

void MainWindow::vUpdatePortList()
{
ui->textEdit->append("Hallo");
}


Isn't there any Click-Signal in QT?

Thank you for helping me.
Best regards, P51D

d_stranz
30th November 2016, 16:40
Isn't there any Click-Signal in QT?

Not for QComboBox. The event filter is the best way to go. As the other post said, you probably -don't- want to look for the Show event, because that only occurs once, when the combo box is first made visible. The way you look for a "click" is to watch for a pair of MouseButtonPress and MouseButtonRelease events that occur within a short time period, or by looking for the FocusIn event inside of your event filter.