我有一个Qt GUI应用程序和一个包含while循环的方法(不在类中)。在while循环的每次迭代中,我都希望将计数器传递到GUI上的文本框。这可能吗?
超级简化的代码:
image_processor.cpp:
void Transition::image_processor (int endindex, int counter) {
do {
...
counter++; //update textbox with this each iteration
} while(counter<endindex-1);
}
transition.cpp(GUI):
Transition::Transition(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::Transition)
{
ui->setupUi(this);
ui->imageCounter->setText("0");
}
Transition::~Transition()
{
delete ui;
}
void Transition::on_runButton_clicked()
{
image_processor(endindex, counter);
}
counter
是int,因此不能将其设置得这么简单,您应该将其转换为QString
。QString
有特殊的静态方法QString::number()
可以做到这一点。
试试这个:
void Transition::image_processor(int endindex, int counter){
do {
...
counter++; //update textbox with this each iteration
ui->imageCounter->setText(QString::number(counter));
} while(counter<endindex-1)
}
你有错误:
void Transition::on_runButton_clicked()
{
void Transition::image_processor(endindex, counter);
}
应该:
void Transition::on_runButton_clicked()
{
image_processor(endindex, counter);
}
你不应该使用 void
我认为这只是一个错字,但是:
Transition::Transition(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::Transition)
{
ui->setupUi(this);//should be
ui->imageCounter->setText("0");
}//should be
如您所见,我ui->setupUi(this);
再次添加,我认为这只是一个错字,但您应该添加此内容。如果不这样做,将不会创建来自Qt Designer的元素,并且会出现错误。
编辑:
transition.h(标头):
//here can be everything, I don't know, you don't post here header but you should add method;
//...
private:
void Transition::image_processor(int endindex, int counter)
transition.cpp(GUI):
Transition::Transition(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::Transition)
{
ui->setupUi(this);
ui->imageCounter->setText("0");
}
Transition::~Transition()
{
delete ui;
}
void Transition::on_runButton_clicked()
{
image_processor(endindex, counter);
}
void Transition::image_processor(int endindex, int counter){
do {
...
counter++; //update textbox with this each iteration
ui->imageCounter->setText(QString::number(counter));
} while(counter<endindex-1)
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句