0

How can I update listWidget from another class?

I am calling method showMessageName() from another class method:

   {
    MainWindow guiObj;     
    guiObj.showMessageName(somedata);
    }



   void MainWindow::showMessageName(QString msgName)
   {
         ui->listWidget->addItem(msgName);

         qDebug() <<"test"<< msgName;
         return ;

   }

This does not update my ui but following code does:

 MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);
    //ui->listWidget->addItem("asdf"); this update is fine

    }

I cant access ui in another class, so I used the aoter option but it has not updated my data.

6
  • If you update your showMessageName function to take a const QString& does that help?
    – Tas
    Aug 24, 2015 at 23:06
  • I tried keeping ui->listWidget->addItem("asdf"); .. But this also doesnot show anytext on listWidget. Aug 24, 2015 at 23:27
  • Also, read this link.. But now it crashes my application and UI doesnot appear.. Aug 24, 2015 at 23:28
  • link: qtforum.org/article/27688/… Aug 24, 2015 at 23:28
  • It looks you have created your MainWindow object as a local variable. It will get destroyed as soon as the method returns. Use a MainWindow pointer and create an object in heap using new Aug 25, 2015 at 0:45

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.