0
void MyAnotherClass::mySlot(){
MainWindow window;
window.myFunction();}

void MainWindow::myFunction(){

qDebug() << "THIS qDebug works well but ui do NOT";

ui->textEdit->setText("Why i do not working?");
}

Why qDebug in this situation works fine, but ui->... doesn't? How to fix it?

EDIT: Solution: `QPlainTextEdit *pointer; MainWindow constructor{ pointer=ui->qPlainTextEdit;}

Some Another's class method{ pointer->appendPlainText("It works"); }`

Artur Lodklif
  • 83
  • 1
  • 9

1 Answers1

0

You create new instance of MainWindow class inside MyAnotherClass::mySlot(). When this slots ends this instance is deleted. So you can't see any changes.

void MyAnotherClass::mySlot() {
    MainWindow window;  //new instance created
    window.myFunction();
} //here this instance deleted

Instead of this you should have pointer to your main window somewhere inside your MyAnotherClass:

MyAnotherClass
{
     .......   
    private slots:
        void mySlot();

    private:
        MainWindow* _mainWindow;
      ...............
};

and then you can do somethins like this:

void MyAnotherClass::mySlot() {
    _mainWindow->myFunction();
}

Of course you should somehow init this pointer before you can use it.

Evgeny
  • 3,910
  • 2
  • 20
  • 37
  • How to init this pointer? Do you know good way to do this? `class MyAnotherClass : public QPlainTextEdit{ private: MainWindow* _mainWindow; }` – Artur Lodklif May 13 '17 at 19:08
  • There could be several ways depends on your code design. You can pass it into constructor, or some method. Or you can make `MainWindow` a singleton. – Evgeny May 13 '17 at 19:13
  • [mainwindow.cpp](https://codepaste.net/ebe9mm) [mainwindow.h](https://codepaste.net/g3nf65) [myqplaintextedit.h](https://codepaste.net/ba8kjc) My another class is **MyQPlaintTextEdit** and code you can find in **mainwindow.cpp** in **exit_slot()** and **on_pushButton_clicked()**; Can you tell me how can I use that pointer in my case? – Artur Lodklif May 13 '17 at 19:41