QProgressBar 连接 类 - "no matching function for call" 之间的进度
QProgressBar connect progress between classes - "no matching function for call"
我在正确设置信号和槽之间遇到了一些麻烦,其中一个 class 我在其中进行了一些计算,而另一个 class 包含带有我的进度条的 GUI。我对 qt 的经验很少,所以我对 signal/slots 的工作方式不太满意。我尝试了一些手册和教程,但我仍然不知道如何设置它。
让我们调用进度条classmainwindow
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
class MainWindow: public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void on_pushButton_clicked();
private:
Ui::MainWindow*ui;
};
#endif // LOADING_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "calc.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->progressBar->setValue(0);
calc sender;
connect(&sender, SIGNAL( inprogress(int) ), ui->progressBar, SLOT( setValue(int) ) );
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
calc clc;
clc.doData();
}
信号从计算 class 发出,简称为 calc
calc.h
#ifndef CALC_H
#define CALC_H
#include <QObject>
class calc : public QObject
{
Q_OBJECT
public:
calc(QObject *parent=0);
void doData();
void printResults(int t);
signals:
void inprogress(int progr);
};
#endif // CALC_H
calc.cpp
#include "calc.h"
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
int t = 0;
int t_end = 100;
int progr = 0;
void calc::printResults(int t)
{
progr = t;
emit inprogress(progr);
QCoreApplication::processEvents(); //Prevent GUI freeze.
}
void calc::doData()
{
for ( int i = 1; i <= t_end; i++ )
{
t++;
printResults(t);
qDebug()<<t;
}
}
calc::calc(QObject *parent)
{
}
Archiev part (code above edited)
The compilation ends with this error:
no matching function for call to 'loading::connect(calc*, const char*,
QProgressBar*&, const char*)
Do I use signals in correct way, or I misunderstand this concept? What's the >correct way to update value of progress bar during this calculations?
编辑: 代码编辑得更清楚,现在显示当前问题 - 信号有效但对 qprogressbar 没有影响。
EDIT2: 现在可以使用了 - 必须调用函数 sender->doData()
。另外正确的分配是 sender = new calc(this)
(calc *sender
添加到 mainwidnow.h 的私有部分)。谢谢大家的帮助,特别是@eyllanesc 指出正确的方法!
只有从 QObject
继承的 类 可以有信号,此外它们必须有宏 Q_OBJECT
,因为这通过 MOC 创建连接所需的所有元类介于信号和槽之间。在你的情况下:
*.h
class calc: public QObject
{
Q_OBJECT
public:
calc(QObject *parent=0);
void signalProgress();
signals:
void inprogress();
};
*.cpp
[...]
calc::calc(QObject *parent): QObject(parent)
{
}
同时通过指针创建一个 calc 实例,因为垃圾收集器将在您使用构造函数后删除该数据。
*.h
private:
Ui::loading *ui;
calc *sender;
*.cpp
loading::loading(QWidget *parent) :
QDialog(parent),
ui(new Ui::loading)
{
ui->setupUi(this);
ui->progressBar->setValue(0);
sender = new calc(this);
connect(sender, SIGNAL( inprogress(int) ), ui->progressBar, SLOT( setValue(int) ) );
}
您需要使用基于 QObject 的 class 才能建立连接
在你的 calc 头文件中,你必须从 QObject 继承,然后添加 Q_OBJECT 宏
calc.h
class calc : pubilc QObject
{
Q_OBJECT
public:
calc();
void signalProgress();
signals:
void inprogress(int progr);
};
有大量使用 Qt 信号和槽的示例和文档,您可以开始阅读 here
P.S.
QProgressBar 也是基于对象的 class 所以这里已经实现了相同的逻辑。
我在正确设置信号和槽之间遇到了一些麻烦,其中一个 class 我在其中进行了一些计算,而另一个 class 包含带有我的进度条的 GUI。我对 qt 的经验很少,所以我对 signal/slots 的工作方式不太满意。我尝试了一些手册和教程,但我仍然不知道如何设置它。
让我们调用进度条classmainwindow
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
class MainWindow: public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void on_pushButton_clicked();
private:
Ui::MainWindow*ui;
};
#endif // LOADING_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "calc.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->progressBar->setValue(0);
calc sender;
connect(&sender, SIGNAL( inprogress(int) ), ui->progressBar, SLOT( setValue(int) ) );
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
calc clc;
clc.doData();
}
信号从计算 class 发出,简称为 calc
calc.h
#ifndef CALC_H
#define CALC_H
#include <QObject>
class calc : public QObject
{
Q_OBJECT
public:
calc(QObject *parent=0);
void doData();
void printResults(int t);
signals:
void inprogress(int progr);
};
#endif // CALC_H
calc.cpp
#include "calc.h"
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
int t = 0;
int t_end = 100;
int progr = 0;
void calc::printResults(int t)
{
progr = t;
emit inprogress(progr);
QCoreApplication::processEvents(); //Prevent GUI freeze.
}
void calc::doData()
{
for ( int i = 1; i <= t_end; i++ )
{
t++;
printResults(t);
qDebug()<<t;
}
}
calc::calc(QObject *parent)
{
}
Archiev part (code above edited) The compilation ends with this error: no matching function for call to 'loading::connect(calc*, const char*, QProgressBar*&, const char*) Do I use signals in correct way, or I misunderstand this concept? What's the >correct way to update value of progress bar during this calculations?
编辑: 代码编辑得更清楚,现在显示当前问题 - 信号有效但对 qprogressbar 没有影响。
EDIT2: 现在可以使用了 - 必须调用函数 sender->doData()
。另外正确的分配是 sender = new calc(this)
(calc *sender
添加到 mainwidnow.h 的私有部分)。谢谢大家的帮助,特别是@eyllanesc 指出正确的方法!
只有从 QObject
继承的 类 可以有信号,此外它们必须有宏 Q_OBJECT
,因为这通过 MOC 创建连接所需的所有元类介于信号和槽之间。在你的情况下:
*.h
class calc: public QObject
{
Q_OBJECT
public:
calc(QObject *parent=0);
void signalProgress();
signals:
void inprogress();
};
*.cpp
[...]
calc::calc(QObject *parent): QObject(parent)
{
}
同时通过指针创建一个 calc 实例,因为垃圾收集器将在您使用构造函数后删除该数据。
*.h
private:
Ui::loading *ui;
calc *sender;
*.cpp
loading::loading(QWidget *parent) :
QDialog(parent),
ui(new Ui::loading)
{
ui->setupUi(this);
ui->progressBar->setValue(0);
sender = new calc(this);
connect(sender, SIGNAL( inprogress(int) ), ui->progressBar, SLOT( setValue(int) ) );
}
您需要使用基于 QObject 的 class 才能建立连接
在你的 calc 头文件中,你必须从 QObject 继承,然后添加 Q_OBJECT 宏
calc.h
class calc : pubilc QObject
{
Q_OBJECT
public:
calc();
void signalProgress();
signals:
void inprogress(int progr);
};
有大量使用 Qt 信号和槽的示例和文档,您可以开始阅读 here
P.S.
QProgressBar 也是基于对象的 class 所以这里已经实现了相同的逻辑。