Read access violation



  • I would like to access the widgets of the parent window directly how do i do this?

    my code compiles but when i run it and reaches the section

     parent->ui->plainTextEdit->appendPlainText("Server started");
    

    program crashed with error message

    c:\documents\systemtray\server.cpp:45: error: Exception at 0xf4546a, code: 0xc0000005: read access violation at: 0x0, flags=0x0 (first chance)

    #include "server.h"
    #include "client.h"
    
    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    
    Server::Server(QObject* parent):
        QTcpServer(parent)
    {
    
    
    }
    
    Server::~Server()
    {
        server.close();
    }
    
    void Server::SendData(QString data)
    {
        emit UpdateLog(data);
    }
    
    void Server::StartServer()
    {
    
        if(listen(QHostAddress::Any,1234))
        {
            //emit UpdateLog("Server started.");
            
            MainWindow* parent = qobject_cast<MainWindow*>(this->parent());
            parent->ui->plainTextEdit->appendPlainText("Server started");
        }
        else
        {
            emit UpdateLog("Server not started.");
        }
    }
    
    void Server::incomingConnection(qintptr handle)
    {
        Client *client = new Client(this);
        client->SetSocket(handle);
    
        MainWindow* parent = qobject_cast<MainWindow*>(this->parent());
        parent->ui->plainTextEdit->appendPlainText("Client Instance Created");
    
    }

  • Qt Champions 2016

    hi
    2 things:
    when you use qobject_cast, please check for null.
    MainWindow* parent = qobject_cast<MainWindow*>(this->parent());
    if (parent)
    ...

    Also, ui is private in MainWindow so should not work,

    There are various way to do this.
    I suggest making public function to set the text etc so
    other class do not know that mainwindow has a plainTextEdit.

    Like
    Mainwindow::setLoggerText( QString &Text );

    ps. parent is bad name for variable name as all QObject has one.

    so
    MainWindow* Main= qobject_cast<MainWindow*>(this->parent());
    if (Main) Main->setLoggerText("Client Instance Created");

    --
    For this to work, the Server class should have been create with Qmainwindow as parent something like
    Server * S= new Server(this) where "this" is mainwindow.


  • Moderators

    I would suggest another solution: your Server class should just emit a signal with a string parameter. Then in your main window you connect this signal to a slot in main window and do what ever you need to do with that string. This way your Server class is decoupled from the UI and does not even need to know there is a main window.

    What you're currently doing is very bad design: your Server class knows implementation details of a UI class! If you change your main window (for example you rename the plainTextEdit) you will need to change the Server class as well.


Log in to reply
 

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