Qt从子方法更新文本框

时间:2014-10-16 17:14:26

标签: c++ qt do-while

我有一个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);
}

1 个答案:

答案 0 :(得分:1)

counter是int,所以你不能这么简单地设置它,你应该把它转换为QStringQString具有特殊的静态方法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)
}