0

I'm writting my first program in Qt Designer, and I have problem with changing QLineEdit text to nothing. I want to use for this operation detached thread, so I can continue working in another part of program without waiting for end of Sleep(5000) function. But when I want to compile this code I'm getting 3 errors:

C2065: "ui" : undeclared identifier

C2227: left "->AutoWyl_textPotwierdzenie" must pointing type class/struct/union/generic

C2227: left "->setText" must pointing type class/struct/union/generic

Could you help me with that errors? Thanks in advance.

Here is problematic part of code:


    {...   
        void autowyl_potwierdzenie_reset();
        std::thread reset(autowyl_potwierdzenie_reset);
        reset.detach();
    }
    
    void autowyl_potwierdzenie_reset()
    {
        Sleep(5000);
        ui->Autowyl_textPotwierdzenie->setText(""); //3 errors
    }
Community
  • 1
  • 1
km2442
  • 779
  • 2
  • 11
  • 31
  • I usually don't use QtDesigner, so not much to tell you here. But why aren't you using a `QTimer` instead of a thread? And why are not you using a `QThread` instead of a `std::thread`? – Miki Jul 07 '15 at 16:05
  • Also this may help http://stackoverflow.com/questions/17615691/qt-c-ui-undeclared-identifier-when-in-new-function – Miki Jul 07 '15 at 16:07
  • @Miki, I used std::thread because i know it from console programing, but thanks I will try to learn it. I'm beginner in QT, so I didn't heard about QTimer. - But now I'll check this out. Your link [I think] will be helpfull. Thanks for help :) I will write, If my problem is solved. – km2442 Jul 07 '15 at 16:27
  • I don't know how to modify this code. Everything not working. Could you help me with this? – km2442 Jul 07 '15 at 18:33
  • I can't right now, maybe tomorrow :D. My advice is to start with a very very simple project, just displaying a QLabel for example. Then add a QPushButton that change the label text on click. Then you should be on your way to do what you want... – Miki Jul 07 '15 at 18:42
  • Also, have a look at these: http://doc.qt.io/qt-4.8/designer-manual.html and http://doc.qt.io/qt-4.8/designer-quick-start.html – Miki Jul 07 '15 at 18:43
  • hm, in other parts of my program i do those operations and it works. So if you could help me toomorow I'll be happy :) – km2442 Jul 07 '15 at 20:30
  • If you need help, SO is the right place. But you should do your part: post a [mcve](http://stackoverflow.com/help/mcve) so others can help you. – Miki Jul 07 '15 at 23:32

2 Answers2

3

Seems like you forgot to scope your method to the class ui is a member of.

Something like this should work:

void MainWindow::autowyl_potwierdzenie_reset()
{
    Sleep(5000);
    ui->Autowyl_textPotwierdzenie->setText("");
}

Where MainWindow is the class containing ui.

A more complete example, that compiles, would help you get an answer more quickly.

This may be of interest: Why does C++ need the scope resolution operator?

Community
  • 1
  • 1
AntonyG
  • 861
  • 9
  • 22
1

I think you are creating the thread from a method of a class containing ui (which is probably a pointer to the Ui::TheWidgetYouDesignedInQtDesigner). On the other hand, the function that the thread runs is out of the class, thus it can't reach ui.

I suggest passing ui as a parameter, or, better, passing only the QLineEdit:

// ...
std::thread reset(autowyl_potwierdzenie_reset, ui-> Autowyl_textPotwierdzenie);
// ...

void autowyl_potwierdzenie_reset(QLineEdit* lineEdit)
{
    Sleep(5000);
    lineEdit->setText("");
}

If autowyl_potwierdzenie_reset is never used again, you can use a lambda function instead (increasing the readability of your code):

std::thread reset([ui]() {
  Sleep(5000);
  ui->Autowyl_textPotwierdzenie->setText("");
});
reset.detach();
cbuchart
  • 10,847
  • 9
  • 53
  • 93
  • Hello, It's a 1.5 year question, I have solved it long time ago, but thank you, I accept this answer :) – km2442 Mar 30 '17 at 07:29