Reputation: 955
I have a class that need to access a QDialog
member, but I can't make it so that the class can see what it needs to. Just to give some background: the ultimate goal is to make a QTextEdit widget fade out and become hidden after 5 seconds of being shown. Here's my code (only the relevant bits; I'm asking more about the scope issue than anything):
dialog.h
#ifndef DIALOG_H
#define DIALOG_H
#include <QDialog>
#include "mytimer.h"
namespace Ui {
class Dialog;
}
class Dialog : public QDialog
{
Q_OBJECT
public:
explicit Dialog(QWidget *parent = 0);
~Dialog();
private:
Ui::Dialog* ui;
myTimer mTimer;
};
#endif // DIALOG_H
dialog.cpp
#include "dialog.h"
#include "ui_dialog.h"
#include "q_debugstream.h"
#include <iostream>
#include <QTextEdit>
Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
ui->setupUi(this);
// this works in this .cpp file but not in mytimer.cpp when trying something similar
ui->someQTextEdit->setTextInteractionFlags(ui->someQTextEdit->textInteractionFlags() | Qt::TextEditable);
myTimer mTimer;
}
Dialog::~Dialog()
{
delete ui;
}
mytimer.h
#ifndef MYTIMER_H
#define MYTIMER_H
#include <QtCore>
class myTimer : public QObject
{
Q_OBJECT
public:
myTimer();
QTimer* timer;
public slots:
void mySlot();
};
#endif // MYTIMER_H
mytimer.cpp
#include "mytimer.h"
#include <QDebug>
#include <iostream>
#include <QTextEdit>
#include <QGraphicsOpacityEffect>
#include <QGraphicsItem>
#include <QPropertyAnimation>
#include <QTime>
#include "ui_dialog.h"
#include "dialog.h"
myTimer::myTimer()
{
timer = new QTimer(this);
connect(timer,SIGNAL(timeout()),this,SLOT(mySlot()));
timer->start(5000);
}
void myTimer::mySlot(){
QGraphicsOpacityEffect *eff = new QGraphicsOpacityEffect(this);
ui->someQTextEdit->setGraphicsEffect(eff);
QPropertyAnimation* a = new QPropertyAnimation(eff,"opacity");
a->setDuration(350);
a->setStartValue(1);
a->setEndValue(0);
a->setEasingCurve(QEasingCurve::OutBack);
a->start(QPropertyAnimation::DeleteWhenStopped);
}
The problem is with the ui->someQTextEdit->setGraphicsEffect(eff);
line. The error I get tells me that ui
is not declared in this scope. someQTextEdit
refers to the QTextEdit
widget I made while creating the dialog in designer. What do I have to do in mytimer.cpp
to be able to access someQTextEdit
(which is defined in the dialog's ui_dialog.h
file that gets generated as a result of using the designer).
Thanks in advance.
Upvotes: 0
Views: 288
Reputation: 38161
Your timer is used as local variable located on stack not on the heap. So when Dialog::Dialog
constructor ends its execution this object is immediately destroyed (basics of C++).
Anyway you don't need custom timer. Use QProperty
animation.
Upvotes: 1