[SOLVED]Qt Widget Pointer to class



  • Hi

    i have an old console application which shall not be changed! but i want to change the output class so that a listwidget will add the output as new items

    so this widget will call the old classes these will call the output class how do i connect the output class with the widget to add items to the listwidget?

    Construct

    MainWindow->calls->old classes->call occasionally->output class->adds item in listwidget of MainWindow

    code of output class:
    @output::output()
    {
    }

    void output::send(std::string text){
    ///*

    QString qtext=QString::fromStdString(text);
    
    QObject::connect(this,SIGNAL(sends(QString)),&w,SLOT(set_protocol(QString)));    //&w is not known !
    
    emit this->sends(qtext);
    //*/
    
    ///*
    std::cout<<text<<std::endl;
    //*/
    

    }@



  • solved this with global pointer to MainWindow

    @MainWindow * pw;

    output::output()
    {
    }

    void output::send(std::string text){
    ///*
    QString qtext=QString::fromStdString(text);
    QObject::connect(this,SIGNAL(sends(QString)),pw,SLOT(set_protocol(QString)));
    emit this->sends(qtext);
    QObject::disconnect(this,SIGNAL(sends(QString)),pw,SLOT(set_protocol(QString)));

    //*/
    
    ///*
    std::cout<<"hi "<<text<<std::endl;
    //*/
    

    }@



  • Why not just connect from the MainWindow to the output::sends signal?

    This is how signals and slots are supposed to be used in the first place (the receiver connects to the sender, not the other way round).

    As your design already introduces a dependecy on MainWindow in output, you do not need a signal at all, just use <code>pw->set_protocol(qtext)</code>. The triumvirate connect, emit, disconnect already screams that you meant calling a method in the first place.



  • thx Lukas

    that was really stupid from me!



  • You're welcome.

    Feel free to change your post title to '[Solved] ...' to indicate that your question has been answered and that there is a solution inside.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.