Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    Solved Read access violation

    General and Desktop
    3
    3
    5416
    Loading More Posts
    • 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
      rafael last edited by

      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 Reply Quote 0
      • mrjj
        mrjj Lifetime Qt Champion last edited by mrjj

        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 Reply Quote 0
        • jsulm
          jsulm Lifetime Qt Champion last edited by

          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 Reply Quote 1
          • First post
            Last post