Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Read access violation

Read access violation

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 3 Posters 5.9k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    rafael
    wrote on last edited by
    #1

    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");
    
    }
    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #2

      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.

      1 Reply Last reply
      0
      • jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #3

        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.

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        1

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved