0

I am trying to call a method called display_txt() from my_class. This method is supposed to change some text in a label. But the method doesn't make any changes to the ui. I know the method runs due to the qDebug(). No errors occur, but the label just doesn't change to "text changed". display_txt() works fine and changes the label when called from the main widget class. There is a similar question in the forums but I couldn't use those solutions. I make an object when the button btn is pressed.

Here is the code:

widget.h

#include <QWidget>
namespace Ui {
class Widget;
}

class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
~Widget();
void display_txt();

private slots:
void on_btn_clicked();

private:
Ui::Widget *ui;
};

my_class.h

#include <QWidget>
#include "widget.h"

class my_class : public QWidget
{
Q_OBJECT
public:
explicit my_class(QWidget *parent = 0);
void test(Ui::Widget ui1);
signals:
private:
Widget *wo = new Widget;
};

Widget.cpp

#include "widget.h"
#include "ui_widget.h"
#include <QDebug>
#include "my_class.h"

Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);

//display_txt();
qDebug() << "Widget object created";

}
void Widget::display_txt(){
ui->lbl->setText("text changes!!!!!");
qDebug() << "display_txt() method finished";

}
void Widget::on_btn_clicked()
{
my_class mc;
}

my_class.cpp

#include "my_class.h"
#include "QDebug"

my_class::my_class(QWidget *parent) : QWidget(parent)
{
qDebug() << "myclass ran";
wo->display_txt();
}
David Yee
  • 3,515
  • 25
  • 45
Kash
  • 1
  • 1
  • So you have tried `ui->lbl->repaint()`? *There is a similar question in the forums but I couldn't use those solutions.* -Which ones and why? – Captain Giraffe Jan 27 '16 at 17:49
  • My guess is that `Widget` needs to have its parent set. Initialize `wo` properly to `new Widget(this)` in the `my_class`'s constructor. – LogicStuff Jan 27 '16 at 17:52
  • http://stackoverflow.com/questions/17436205/c-qt-proper-way-to-access-ui-from-another-class-in-qt-edited - I tried this but no luck. I also tried repaint() and setting the parent class to new widget(this) in my_class constructor with no luck either. – Kash Jan 27 '16 at 18:20

0 Answers0