문제

I have a class :

class gameClientInteraction : public QMainWindow

with, in the .h :

private :
QTextEdit* console; 

my constructor is like this :

gameClientInteraction::gameClientInteraction()
{ 
// Menus
QMenu *menuFichier = menuBar()->addMenu("&Jeu");
QAction *actionQuitter = new QAction("&Quitter", this);
menuFichier->addAction(actionQuitter);
connect(actionQuitter, SIGNAL(triggered()), qApp, SLOT(quit()));


// View
QGraphicsView *theGraphicsView = new QGraphicsView(this);
theGraphicsView->setFixedSize(605,605);

QTextEdit* console = new QTextEdit(this);
console->setGeometry(0,625,600,100);
console->setReadOnly(true);
console->append("Bienvenue !");

setCentralWidget(theGraphicsView);

//Scene
theGraphicsView->setScene(m.afficheMap());//afficheMap give me a QGraphicsScene*
}

I have this function that crash my program when I launch it (it 's okay when I comment the instruction).

   void gameClientInteraction::msgConsole(QString msg){
   console->append(msg);
}

So why is it crashing with this instruction?

도움이 되었습니까?

해결책

You've hidden the class member variable console in your constructor by declaring a local pointer with the same name. In other words, this:

QTextEdit* console = new QTextEdit(this);

should be this:

console = new QTextEdit(this);

As an alternative, consider using an initialization list:

gameClientInteraction::gameClientInteraction() : console(new QTextEdit(this))
{
    // constructor code goes here

    console->setGeometry(0,625,600,100);
    console->setReadOnly(true);
    console->append("Bienvenue !");
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top