I'm trying to make some custom slots but it complains that the custom slots don't exist.
I have googled but can't find anything with a similar situation. No solutions worked for me.
QObject::connect: No such slot QSlider::setMinimum(int)
inkpuppet.cpp
#include "inkpuppet.h"
#include "ui_inkpuppet.h"
#include "aboutdialog.h"
#include <QDialog>
#include <QWidget>
#include <QtCore>
#include <QtGui>
#include <QButtonGroup>
#include <QSlider>
InkPuppet::InkPuppet(QWidget *parent) :
QWidget(parent),
ui(new Ui::InkPuppet)
{
ui->setupUi(this);
connect(ui->lowerFrameBox, SIGNAL(valueChanged(int)), ui->timeSlider, SLOT(setMinimum(int)));
connect(ui->upperFrameBox, SIGNAL(valueChanged(int)), ui->timeSlider, SLOT(setMaximum(int)));
//connect(ui->lowerFrameBox, SIGNAL(valueChanged(int)), ui->timeSlider, SLOT(setRange(int,int)));
}
InkPuppet::~InkPuppet()
{
delete ui;
}
void InkPuppet::on_aboutButton_clicked()
{
}
void InkPuppet::setMinimum(int value)
{
ui->timeSlider->setMinimum(value);
}
void InkPuppet::setMaximum(int value)
{
ui->timeSlider->setMaximum(value);
}
inkpuppet.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QtCore>
#include <QtGui>
#include "aboutdialog.h"
namespace Ui {
class InkPuppet;
}
class InkPuppet : public QWidget
{
Q_OBJECT
public:
explicit InkPuppet(QWidget *parent = 0);
~InkPuppet();
public slots:
void on_aboutButton_clicked();
void setMinimum(int value);
void setMaximum(int value);
private:
Ui::InkPuppet *ui;
AboutDialog *aDialog;
};
#endif // WIDGET_H
You try to connect your ui->lowerFrameBox valueChanged(int) signal with a setMinimum slot in ui->timeSlider. However, the setMinimum there is no slot. You need to connect to the slot in InkPuppet.
connect(ui->lowerFrameBox, SIGNAL(valueChanged(int)),
this, SLOT(setMinimum(int)));
In Qt 5.1 you can use connect in these ways too :
connect(ui->upperFrameBox,&QSlider::valueChanged,this,&InkPuppet::setMaximum);
connect(ui->upperFrameBox,&QSlider::valueChanged,&InkPuppet::setMaximum);
or even you can use c++11 lambda feauture.
connect(ui->upperFrameBox,&QSlider::textChanged,
[&](int value) {ui->timeSlider->setMaximum(value);});
Related
I'm using the Qt application. When I'm trying to call the subclass from the connect, subclass (doWork) function it's not getting called. please refer to my sample code and help me to solve the issue.
I'm using the Qt application. When I'm trying to call the subclass from the connect, subclass (doWork) function it's not getting called. please refer to my sample code and help me to solve the issue.
MainWindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include "QMainWindow"
#include <QThread>
#include "Worker.h"
namespace Ui { class MainWindow; }
class MainWindow: public QMainWindow {
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow(); private:
Ui::MainWindow *ui;
QThread *thread;
Worker *worker;
signals:
void requestUpdate(int initial);
};
#endif // MAINWINDOW_H
Mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
thread = new QThread();
worker = new Worker();
worker->moveToThread(thread);
connect(this, SIGNAL(requestUpdate(int)), worker, SLOT(doWork(int)));
int enc = 10;
emit requestUpdate(enc);
}
MainWindow::~MainWindow()
{
delete thread;
delete worker;
delete UI;
}
Worker.h
#ifndef WORKER_H
#define WORKER_H
#include<QObject>
class Worker: public QObject
{
Q_OBJECT
signals:
void workRequested();
public slots:
void doWork(int initial);
};
#endif // WORKER_H
Worker.cpp
#include "Worker.h"
#include <QTimer>
#include <QEventLoop>
#include <QThread>
#include <QDebug>
Worker::Worker(QObject *parent) :
QObject(parent)
{
_working =false;
_abort = false;
}
void Worker::doWork(int initial)
{
qDebug() << "initial" << endl;
}
Thanks & Regards,
Haji
I wrote a program to control device with serial port. It works fine but I'd like to seperate the class which would operate serial ports. I also want to have acess to buttons, comboboxes, etc. from this class.
Here are most important parts of code:
Serialsettings header:
#ifndef SERIALSETTINGS_H
#define SERIALSETTINGS_H
#include <QMainWindow>
#include <QSerialPort>
#include "mainwindow.h"
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class SerialSettings : public MainWindow
{
Q_OBJECT
public:
struct Settings
{
QString name;
qint32 baudRate;
QString stringBaudRate;
QSerialPort::DataBits dataBits;
QString stringDataBits;
QSerialPort::Parity parity;
QString stringParity;
QSerialPort::StopBits stopBits;
QString stringStopBits;
QSerialPort::FlowControl flowControl;
QString stringFlowControl;
};
explicit SerialSettings(QWidget *parent = nullptr);
~SerialSettings();
Settings settings() const;
signals:
private:
void fillPortsInfo();
void fillPortsParameters();
void updateSettings();
private:
Settings m_currentSettings;
};
#endif
Serialsettings source:
#include "serialsettings.h"
#include <QSerialPort>
#include <QSerialPortInfo>
#include "mainwindow.h"
#include "ui_mainwindow.h"
SerialSettings::SerialSettings(QWidget *parent)
: MainWindow(parent)
{
//some functions
}
mainwindow.h:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QSerialPort>
//#include "serialsettings.h"
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class SerialSettings;
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
Ui::MainWindow *ui;
explicit MainWindow(QWidget *parent = nullptr);
~MainWindow();
// some code
};
#endif
mainwindow.cpp:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "serialsettings.h"
#include <QSerialPortInfo>
#include <QSerialPort>
#include <QString>
#include <QDebug>
#include <QTimer>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
//some code
};
MainWindow::~MainWindow()
{
delete ui;
}
I don't have any error at this moment but it finishes with: The program has unexpectedly finished.
Could you help me please with this problem?
I have one MainWindow and one Class; I want to send data between them using custom signal and slot. I can't seem to figure it out, I need help.
Here is my code:
MainWindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <iostream>
#include "receive.h"
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
signals:
void sendit(QString name);
private slots:
void on_send_button_clicked();
void display(QString e)
{
std::cout<<"Here is where I am called this "<<e.toStdString()<<std::endl;
}
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
MainWindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "receive.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
Receive *r = new Receive();
connect(this, SIGNAL(sendit(QString)), r, SLOT(display(QString)));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_send_button_clicked()
{
emit sendit(ui->lineEdit->text());
}
receive.h
#ifndef RECEIVE_H
#define RECEIVE_H
#include <iostream>
#include <QDialog>
class Receive : public QDialog
{
public:
Receive();
private slots:
void display(QString e);
};
#endif // RECEIVE_H
receive.cpp
#include "receive.h"
#include "mainwindow.h"
Receive::Receive()
{
}
void Receive::display(QString e)
{
std::cout<<"Here is where I am called this "<<e.toStdString()<<std::endl;
}
When I run this program, I get this message:
06:26:29: Starting C:\Users\Troy\Documents\build-tests-Desktop_Qt_5_14_1_MinGW_32_bit-Debug\tests.exe ...
QObject::connect: No such slot QDialog::display(QString) in ..\tests\mainwindow.cpp:11
QObject::connect: (sender name: 'MainWindow')
How do I get this done, please?
Thank you for your help.
Your slot in Receive needs to be public not private. Much the same as other class members, private slots can only be used by the class itself.
If you use the modern connect syntax you'll get a better compile time error:
connect(this, &MainWindow::sendit, r, &Receive::display);
You also need to make sure you add Q_OBJECT to every Qt class, it is missing from Receive.
I have a problem with my Qt code. I wanted to write an program which are taking a coordinates of point and then that point is drawing in point with that coordinates. My program doesn't have any errors or warnings when i built it but it's crashing at start(MainWindow doesn't show). This is my code:
main.cpp
#include < QApplication >
#include "mainwindow.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
MainWindow win;
win.show();
return app.exec();
}
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QWidget>
#include <QAction>
#include <QToolBar>
#include <QTextCodec>
#include <QObject>
#include <QDialog>
#include <QLineEdit>
#include <QPushButton>
#include <QString>
#include "addpoint.h"
class MainWindow: public QMainWindow
{
Q_OBJECT
private:
QToolBar *AddToolbar;
QAction *AddPointAction;
AddPoint *AddPointDialog;
QLineEdit *x;
public:
MainWindow();
void createToolbar();
void createActionAdd();
signals:
public slots:
void PointClicked();
void DialogAccepted();
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
MainWindow::MainWindow()
{
QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF-8"));
createActionAdd();
createToolbar();
connect(AddPointAction, SIGNAL(triggered(bool)), this, SLOT(PointClicked()));
connect(AddPointDialog, SIGNAL(accepted()), this, SLOT(DialogAccepted()));
setMinimumSize(480, 320);
}
/**/
void MainWindow::createToolbar()
{
AddToolbar = new QToolBar;
AddToolbar->addAction(AddPointAction);
addToolBar(AddToolbar);
}
/**/
void MainWindow::createActionAdd()
{
AddPointAction = new QAction("Add Road", this);
x = new QLineEdit(this);
x->setFixedSize(100, 30);
x->move(100, 100);
}
/**/
void MainWindow::PointClicked()
{
AddPointDialog = new AddPoint(this);
AddPointDialog->setModal(true);
AddPointDialog->exec();
}
/**/
void MainWindow::DialogAccepted()
{
x->setText("abc");
}
addpoint.h
#ifndef ADDPOINT_H
#define ADDPOINT_H
#include <QWidget>
#include <QTextCodec>
#include <QDialog>
#include <QObject>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QString>
class AddPoint : public QDialog
{
Q_OBJECT
private:
QLabel *XpointL;
QLineEdit *XPoint;
QPushButton *OKButton;
public:
AddPoint(QWidget *parent);
void createButton();
signals:
public slots:
};
#endif // ADDPOINT_H
addpoint.cpp
#include "addpoint.h"
AddPoint::AddPoint(QWidget *parent) :QDialog(parent)
{
QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF-8"));
createButton();
connect(OKButton, SIGNAL(clicked(bool)), this, SLOT(accept()));
setMinimumSize(320, 240);
}
/**/
void AddPoint::createButton()
{
XpointL = new QLabel("Point X:", this);
XpointL->setFixedSize(100, 30);
XpointL->move(10, 10);
XPoint = new QLineEdit(this);
XPoint->setFixedSize(180, 30);
XPoint->move(120, 10);
OKButton = new QPushButton("OK", this);
OKButton->setFixedSize(100, 30);
OKButton->move(200, 150);
}
After running the program i see in aplication output lap:
"The program has unexpectedly finished."
"C:\QT\build-xxx-Desktop_Qt_5_4_2_MSVC2013_64bit-Debug\debug\xx.exe crashed"
I note that i made some experiments with this code and i saw that i have problem with signal accpeted() at mainwindow.cpp. I don't know what can i do with this problem. I hope you will help me.
AddPointDialog is uninitialized pointer, it does not yet point to valid AddPoint in MainWindow's constructor. You cannot call connect on that. Its value will change later, when you do AddPointDialog = new AddPoint(this); and only then will you be able to call connect.
Simply, you should move your connect call to void MainWindow::PointClicked() after you've initialized your pointer. I'd also make AddPointDialog local to that function and store it on the stack (you don't use it anywhere else and you're leaking memory).
The code would be:
void MainWindow::PointClicked()
{
AddPoint AddPointDialog(this);
AddPointDialog.setModal(true);
connect(&AddPointDialog, SIGNAL(accepted()), this, SLOT(DialogAccepted()));
AddPointDialog.exec();
}
I have a class called dosecalibration which contains dosecalibration.cpp and dosecalibration.h. The class is associated to a separate ui form. On the form, when a button is clicked, a signal is emitted.
There is a connection within main window to receive this signal, however it doesn't seem to be working. The code is as following:
dosecalibration.h :
#ifndef DOSECALIBRATION_H
#define DOSECALIBRATION_H
#include <QDialog>
#include <QDebug>
namespace Ui {
class dosecalibration;
}
class dosecalibration : public QDialog
{
Q_OBJECT
public:
explicit dosecalibration(QWidget *parent = 0);
~dosecalibration();
double dosefactor;
//bool dose;
private slots:
void on_useCharge_clicked();
void on_useCounts_clicked();
void on_pushButton_clicked();
// void on_pCSB_valueChanged();
// void on_countsSB_valueChanged();
signals:
void applydose();
private:
Ui::dosecalibration *ui;
};
#endif // DOSECALIBRATION_H
dosecalibration.cpp :
#include "dosecalibration.h"
#include "ui_dosecalibration.h"
dosecalibration::dosecalibration(QWidget *parent) :
QDialog(parent),
ui(new Ui::dosecalibration)
{
ui->setupUi(this);
ui->countsSB->setEnabled(false);
ui->countsSB->setValue(ui->pCSB->value()*100/9.6);
}
dosecalibration::~dosecalibration()
{
delete ui;
}
void dosecalibration::on_useCharge_clicked()
{
ui->countsSB->setEnabled(false);
ui->pCSB->setEnabled(true);
}
void dosecalibration::on_useCounts_clicked()
{
ui->pCSB->setEnabled(false);
ui->countsSB->setEnabled(true);
}
void dosecalibration::on_pushButton_clicked()
{
if(ui->useCharge->isChecked()){
dosefactor = ui->pCSB->value();
}
else if(ui->useCounts->isChecked()){
dosefactor = ui->countsSB->value();
}
emit applydose();
}
//void dosecalibration::on_pCSB_valueChanged()
//{
// ui->countsSB->setValue(ui->pCSB->value()*100/9.6);
//}
//void dosecalibration::on_countsSB_valueChanged()
//{
// ui->pCSB->setValue(ui->countsSB->value()*9.6/100);
//}
And mainwindow.h (only included the 'includes' and the slots):
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <AFE_variables.h>
#include <QMainWindow>
#include "FPGA/fpga.h"
#include "FPGA/okFrontPanelDLL.h"
#include "decoder.h"
#include "analysis.h"
#include "about.h"
#include "logfile.h"
#include "Graph/graphicsscene.h"
#include "Graph/graphdialog.h"
#include "Graph/qcustomplot.h"
#include "Graph/graphicsview.h"
#include "settingsdialog.h"
#include "dosecalibration.h"
#include <QFileDialog>
#include <QProgressBar>
#include <QGraphicsScene>
#include <QGraphicsRectItem>
#include <QGraphicsTextItem>
#include <QDialog>
#include <QDebug>
#include <math.h>
slots:
//Dose calibration
void InitialiseGraphsAfterDose();
void on_actionDose_Calibration_4_triggered();
//void doseCalibrationEnabled();
private:
//Initalisation
void onStart();
void AllocateMemory();
void ConnectFPGA();
bool CheckConnection();
Ui::MainWindow *ui;
okCFrontPanel *xem;
FPGA *fpga;
QProgressBar *progressBar;
QTimer *PlayTimer;
LogFile *logfiledialog;
LogFile *logfileanalysis;
QString LoadLogFilePath;
dosecalibration *dose;
and a snippet from graphing.cpp, which is a part of the main window class:
//connects for dose calibration
dose = new dosecalibration(this);
connect(dose,SIGNAL(applydose()),this,SLOT(InitialiseGraphsAfterDose()));
}
void MainWindow::InitialiseGraphsAfterDose()
{
apply_dose = true;
InitialiseGraphs();
qDebug() << apply_dose;
}
So the applydose() signal is emitted at the push of a button in the dosecalibration ui. The connect should mean that the value of apply_dose is sent to console, however nothing is displayed.
EDIT:
Placing the connect within an if statement to determine if it is truly connecting confirms that it is indeed working correctly.
//connects for dose calibration
dose = new dosecalibration(this);
if(connect(dose,SIGNAL(applydose()),this,SLOT(InitialiseGraphsAfterDose())))
{
qDebug() << "connect worked";
}
}
The code above successfully outputs the message confirming the connect.
Any idea?
Remove the multiple instances of dosecalibration, or make sure to connect each one of those, if you really need multiple instances.