How can i emit signal from another class? In my implementation shown below I've got "unresolved external symbol error" when I try to emit signal in SerialPort::open method.
Code, head file of main window:
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
std::shared_ptr<SerialPort> serialPort;
private slots:
void labelchange();
private:
Ui::MainWindow *ui;
};
and the cpp file:
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow),
serialPort(std::shared_ptr<SerialPort>(new SerialPort))
{
ui->setupUi(this);
connect(serialPort.get(), SIGNAL(opened()),this,SLOT(labelchange()));
}
void MainWindow::labelchange()
{
ui->testinLabel->setText("signal connected to slot");
}
and the other class method when i try emit signal:
head file:
class SerialPort : public QObject
{
public:
SerialPort();
void open()
signals:
void serial_opened();
}
and cpp file:
void SerialPort::open()
{
emit serial_opened();
}
This is just normal signal emitting from a class. Not "from another" class.
You are missing the Q_OBJECT macro, QObject do not really work with out it:
class SerialPort : public QObject
{
Q_OBJECT
public:
SerialPort();
void open()
signals:
void serial_opened();
}
And you need to have the file processed by moc. (happens automatically, if the files are listed in the .pro file)
SerialPort shall contain Q_OBJECT marco like this:
class SerialPort : public QObject
{
Q_OBJECT
public:
SerialPort();
void open()
signals:
void serial_opened();
}
Also please check your .pro file and check whether you added your SerialPort.h under HEADERS section.
Related
I am using Qt 5.15.1 and C++ to create a simple app which processes signals from hardware and displays images and driver status. I want to update the statusbar message when an int value defined in another class changes. I would like this to happen automatically, each time this value changes. I understand that I need signals and slots to achieve this. So far I have done the following:
signalprocessing.h
class SignalProcessing: public QObject
{
Q_OBJECT
public:
SignalProcessing(QObject *parent = nullptr);
private:
int status;
public slots:
int GetStatus();
signals:
void StatusChanged();
}
signalprocessing.cpp
SignalProcessing::SignalProcessing(QObject *parent)
: QObject(parent)
{
}
int SignalProcessing::GetStatus()
{
emit(StatusChanged());
return status;
}
mainwindow.h
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
virtual ~MainWindow();
SignalProcessing *signalProcessing;
}
mainwindow.cpp
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
signalProcessing= new SignalProcessing(this);
ui->statusbar->showMessage(QString::number(signalProcessing->GetStatus()));
}
The problem is that the statusbar message is currently not updated automatically, but set to a given value.
How do I make sure it will always display the current status?
The idea is that you want to emit your StatusChanged signal when the value has actually changed, not when you call GetStatus(). So you need a SetStatus() function, and your SignalProcessing class will need to know when to call that. Then you want to connect that signal to a slot that then updates your status bar. It might look something like this:
class SignalProcessing: public QObject
{
Q_OBJECT
public:
SignalProcessing(QObject *parent = nullptr);
int getStatus();
void setStatus(int value);
private:
int status;
signals:
void statusChanged(); // It's better to start signals with a lower case letter
}
signalprocessing.cpp
SignalProcessing::SignalProcessing(QObject *parent)
: QObject(parent)
{
}
int SignalProcessing::getStatus()
{
return status;
}
void SignalProcessing::setStatus(int value)
{
if (status != value)
{
status = value;
emit statusChanged();
}
}
mainwindow.h
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
virtual ~MainWindow();
SignalProcessing *signalProcessing;
public slots:
void updateStatus();
}
mainwindow.cpp
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
signalProcessing= new SignalProcessing(this);
connect(signalProcessing, &SignalProcessing::statusChanged, this, &MainWindow::updateStatus);
updateStatus();
}
void MainWindow::updateStatus()
{
ui->statusbar->showMessage(QString::number(signalProcessing->getStatus()));
}
First change singal declartion so it will provide new value:
class SignalProcessing: public QObject
{
Q_OBJECT
public:
SignalProcessing(QObject *parent = nullptr);
private:
int status;
public slots:
int setStatus(int value);
signals:
void statusChanged(const QString& message);
}
void SignalProcessing::setStatus(int value)
{
if (status != value)
{
status = value;
emit statusChanged(tr("Status is %1").arg(status));
}
}
Then connect this signal to QStatusBar::showMessage slot and you done.
For example I have a three files
the generalsetting.ui, generalsetting.h, generalsetting.cpp .
I want to trigger the enableNotification method when the Notifycheckbox in the UI file is clicked.
by passing the return value of the checkbox to the function
I tried using
connect(ui->notifyCheckBox, &QCheckBox::toggled, this, enableNotification(&QCheckBox::toggled));
and
connect(ui->notifyCheckBox, &QCheckBox::isChecked, this, enableNotification(&QCheckBox::toggled));
it does not work
here is the both the header and source
namespace Ui {
class GeneralSettings;
}
class GeneralSettings : public QWidget
{
Q_OBJECT
public:
explicit GeneralSettings(QWidget *parent = nullptr);
~GeneralSettings() override;
private slots:
void enableNotification(bool enable);
private:
Ui::GeneralSettings *ui;
Utils *utils;
};
the source File
GeneralSettings::GeneralSettings(QWidget *parent) :
QWidget(parent),
ui(new Ui::GeneralSettings)
{
ui->setupUi(this);
//here is where i am adding the connect function.
}
void GeneralSettings::enableNotification(bool enable)
{
utils->settings->setValue("General/notify", enable);
}
PS:i included only the ones that i feel are useful
you can connect to a lambda:
connect(ui->yyy, &QCheckBox::toggled, [this]()
{
qDebug() << "sd.." << ui->yyy->isChecked();
enableNotification(ui->yyy->isChecked());
});
or directly, same way as the signal
connect(ui->yyy, &QCheckBox::toggled,this, &Foo::enableNotification);
in my Qt program I need the dialog to send a signal to a slot in the mainwindow. I have set the connection up correctly to the best of my knowledge and it does not give me any errors during compile or run time but for some reason it just doesn't do anything when the button that is supposed to activate the signal is clicked. Why is this happening?
beastiary.h (mainwindow header)
namespace Ui {
class Beastiary;
}
class Beastiary : public QMainWindow
{
Q_OBJECT
public:
explicit Beastiary(QWidget *parent = 0);
Ui::Beastiary *ui;
QStringList MyList;
~Beastiary();
public slots:
void refresh();
private slots:
void on_pushButton_clicked();
void on_listWidget_itemClicked(QListWidgetItem);
void on_pushButton_2_clicked();
void on_pushButton_3_clicked();
beastiary.cpp (mainwindow cpp file)
Beastiary::Beastiary(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::Beastiary)
{
ui->setupUi(this);
Dialog dialog;
connect(&dialog, SIGNAL(gorefresh()),this, SLOT(refresh())) ;
void Beastiary::refresh()
{
qDebug () << "recieved";
ui->listWidget->clear();
QString path = "C:/Program Files/Bargest/bin";
QDir myPath(path);
myPath.setFilter(QDir::Dirs | QDir::Files | QDir::NoDotAndDotDot);
MyList = myPath.entryList();
ui->listWidget->addItems(MyList);
}
dialog.h
namespace Ui {
class Dialog;
}
class Dialog : public QDialog
{
Q_OBJECT
public:
explicit Dialog(QWidget *parent = 0);
~Dialog();
signals:
void gorefresh();
private slots:
void on_pushButton_done_clicked();
void on_pushButton_cancel_clicked();
void on_pushButton_clicked();
private:
Ui::Dialog *ui;
dialog.cpp
void Dialog::on_pushButton_done_clicked()
{
emit gorefresh();
}
I did leave out large parts of the code that just have nothing to do with the actual signals and slots mechanism at play here.
You're only connecting the dialog instance you created locally in the Bestiary's constructor, which goes out of scope as the constructor finishes.
connect is connecting instances, not classes. That means you need to connect each created dialog:
void Beastiary::on_pushButton_clicked() {
Dialog* ad = new Dialog(this);
connect(ad, SIGNAL(gorefresh()), this, SLOT(refresh()));
ad->show();
}
While at it, you should seriously consider using the type-safe connect syntax that came with Qt 5:
void Beastiary::on_pushButton_clicked() {
Dialog* ad = new Dialog(this);
connect(ad, &Dialog::gorefresh, this, &Bestiary::refresh));
ad->show();
}
What I'm trying to do is to call an time consuming operation (MockClamWrapper::loadDatabase()) in a separate thread at the moment of creation of my window and then to update my window once the operation is completed. Here is the code that I have.
MockClamWrapper.h
class MockClamWrapper : QObject
{
Q_OBJECT
public:
MockClamWrapper();
~MockClamWrapper();
bool loadDatabase(unsigned int *signatureCount=NULL);
Q_SIGNALS:
void databaseLoaded();
};
MockClamWrapper.cpp
bool MockClamWrapper::loadDatabase(unsigned int *signatureCount){
QThread::currentThread()->sleep(10);
databaseLoaded();
return true;
}
MainWindow.h
#include <QMainWindow>
#include <QFileDialog>
#include "mockclamwrapper.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public slots:
void enableWindow();
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
MockClamWrapper *clam;
void initWindow();
};
MainWindow.cpp
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect((QObject*)clam, SIGNAL(databaseLoaded()),(QObject*)this,SLOT(enableWindow()));
QFuture<void> fut = QtConcurrent::run(this,&MainWindow::initWindow);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::initWindow(){
clam->loadDatabase(NULL);
}
void MainWindow::enableWindow(){
ui->checkFileButton->setEnabled(true);
}
The program compiles, but it crashes right after start. I assume that I do something wrong with slots and signals, but can't find my mistake.
The reason for crash is that you are not making any instance of the class MockClamWrapper. In the connect statement, you are referencing a pointer that points to nothing. Make a new object and then connect :
clam = new MockClamWrapper();
connect(clam, SIGNAL(databaseLoaded()), this, SLOT(enableWindow()));
I'm trying to write my new app, but it crashes every time I press a button on QDialog.
Here's my code :
mainwindow.h
#include <QMainWindow>
#include "creatlist.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
QDialog* creatList;
public slots:
void tableFull(){
...some code here...
}
private:
Ui::MainWindow *ui;
};
creatlist.h :
#include <QDialog>
#include "mainwindow.h"
namespace Ui {
class creatlist;
}
class MainWindow;
class creatlist : public QDialog
{
Q_OBJECT
public:
explicit creatlist(QWidget *parent = 0);
~creatlist();
MainWindow* mainwindow;
signals:
void updateList();
public slots:
void ready(){
///////////////////////////////////////////////////////////crash
connect(this,SIGNAL(updateList()),mainwindow,SLOT(tableFull()));
emit updateList();
}
private:
Ui::creatlist *ui;
};
If i try to send some signals my app crashes with a Segmentation Fault.
I did:
void creatlist::ready()
{
mainwindow = new MainWindow(this);
emit mainwindow->linktableFull();
}
but if I try to do QTextBroser.append("hue hue"); in linktableFull(), QTextBrowser is always empty.
Your QTextBrowser always empty because you create new mainwindow object in every ready() function. You should create mainwindow object once and use same mainwindow object throughout code. You can create new mainwindow object in creatlist constructor.