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. [Solved] No access to QMainWindow
Forum Updated to NodeBB v4.3 + New Features

[Solved] No access to QMainWindow

Scheduled Pinned Locked Moved General and Desktop
11 Posts 2 Posters 3.2k Views 1 Watching
  • 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.
  • S Offline
    S Offline
    silep
    wrote on last edited by
    #1

    Hello,

    I have a problem to use the function writeErrorMsg in errorMsg.h because of the use of the parameter MainForm in it. I don't why I can't use it like that..

    The error is:
    @error C2248: 'QMainWindow::QMainWindow' : cannot access private member declared in class 'QMainWindow'@

    And the code:

    errorMsg.h:

    @#include "mainform.h"
    class errorMsg : public QObject
    {
    public:
    errorMsg();
    ~errorMsg();
    void writeErrorMsg(QString msg, QString type_msg, MainForm &Window);
    };
    @

    errorMsg.cpp:

    @#include "errorMsg.h"

    void errorMsg::writeErrorMsg(QString msg, QString type_msg, MainForm &Window)
    {
    QColor colorLine;

    if (type_msg=="type_error")
    {
    colorLine.setRgb(214,30,16);
    }
    Window.insertTextInErrorBrowser(msg, colorLine);
    }@

    mainform.h:

    @class MainForm : public QMainWindow
    {
    Q_OBJECT
    public:
    void insertTextInErrorBrowser(QString msg, QColor colorLine);
    private:
    Ui::MainForm *ui;
    };@

    mainform.cpp:

    @#include "mainform.h"

    void MainForm::insertTextInErrorBrowser(QString msg, QColor colorLine)
    {
    ui->teErrorBrowser->moveCursor(QTextCursor::End);
    ui->teErrorBrowser->setTextColor(colorLine);
    ui->teErrorBrowser->setFontWeight(QFont::Bold);
    ui->teErrorBrowser->insertPlainText(msg + "\n");
    colorLine.setRgb(0,0,0);
    ui->teErrorBrowser->setTextColor(colorLine);
    }@

    Would anyone have an idea about what is wrong about that and how to fix it? Thank you in advance!

    1 Reply Last reply
    0
    • X Offline
      X Offline
      Xander84
      wrote on last edited by
      #2

      Hey again, are you still working on that? Just wondering how many topics you have created in the last weeks haha :D

      Anyway I don't see any method definition in your MainForm.h for the function insertTextInErrorBrowser? You have defined a function "writeDateTimeErrorMsg" and in your cpp file your use insertTextInErrorBrowser with other parameter, is there any relation of the 2 functions.
      You need to put a defintion for insertTextInErrorBrowser in your class, and it needs to be public if you want to call if from your errorMsg class (unless the class is a friend, but I don't want to confuse you with that).

      1 Reply Last reply
      0
      • S Offline
        S Offline
        silep
        wrote on last edited by
        #3

        Hey, yeah the beginning code was not mine, and there was a lot of thing to understand, because I'm not an expert yet^^

        Sorry, it is just a copy paste error, I copied the wrong function into the mainform.h. I corrected it now in the displayed code (insertTextInErrorBrowser)

        1 Reply Last reply
        0
        • X Offline
          X Offline
          Xander84
          wrote on last edited by
          #4

          Yeah lol if you make errors while copying your code it is hard to find the actual errors :D
          Are you sure the code is as you copied it now? Because the function insertTextInErrorBrowser is definitely public and the error "cannot access private member" makes no sense, because it is public. so I guess in your code it is not declared public did you double check that? Would be the first thing you do if you see an error like this of course.

          1 Reply Last reply
          0
          • S Offline
            S Offline
            silep
            wrote on last edited by
            #5

            Yeah sorry but it is correct now. The method insertTextInErrorBrowser is public in my class mainform, I'm 100% sure about that. That why I don't understand the error message, and why I'm asking.

            1 Reply Last reply
            0
            • X Offline
              X Offline
              Xander84
              wrote on last edited by
              #6

              Ah I just noticed it says "QMainWindow::QMainWindow" and that will be the constructor, and I don't see any in your MainForm!? Why did you remove that? if your create a new QMainWindow App Qt Creator will generate the constructor for your, usually. :D

              it should look like
              @
              // header
              explicit MainWindow(QWidget *parent = 0);
              // implementation
              MainWindow::MainWindow(QWidget *parent) :
              QMainWindow(parent),
              ui(new Ui::MainWindow)
              {
              ui->setupUi(this);
              }
              @

              1 Reply Last reply
              0
              • S Offline
                S Offline
                silep
                wrote on last edited by
                #7

                I just didn't write it because I didn't want to write 3 pages of code. It is not cool to read that for you :)

                mainform.h:

                @public:
                explicit MainForm(QWidget *parent = 0);
                ~MainForm();@

                mainform.cpp:

                @MainForm::MainForm(QWidget *parent)
                : QMainWindow(parent),
                ui(new Ui::MainForm)
                {
                ui->setupUi(this);
                }

                MainForm::~MainForm()
                {
                delete ui;
                }@

                1 Reply Last reply
                0
                • X Offline
                  X Offline
                  Xander84
                  wrote on last edited by
                  #8

                  ok well it's hard to debug your code if you get error slike that and can see the code you know :D
                  Anyway the error message should tell you at what line the syntax error is found, can your provide the code around those lines? QMainWindow::QMainWindow can onyl be a constructor so I have no idea what you did, I see you use MSVC sometimes that gives weird error and I try to compile the app with MinGW on windows, that sometimes help to identify errors if you use 2 different compilers and compare the errors messages. :)

                  1 Reply Last reply
                  0
                  • S Offline
                    S Offline
                    silep
                    wrote on last edited by
                    #9

                    The error message is linked to the function declaration of writeErrorMsg in errorMsg.h. I don't know if it helps..

                    1 Reply Last reply
                    0
                    • X Offline
                      X Offline
                      Xander84
                      wrote on last edited by
                      #10

                      I don't know, at first I would say your try to copy the QMainWindow, which is not possible with Qt, QObjects cannot be copied that is why the copy constructor might be private, the copy constructor QMainWindow::QMainWindow.
                      But you are using a reference "MainForm &Window" so that can't copy it, where do you get the reference from? Maybe use a real pointer in this case, the Qt framework uses pointers in such cases, and only const references , this again is where c++ gets complicated with pointers, left value references, r-value references and stuff like that it can get confusing. :(

                      1 Reply Last reply
                      0
                      • S Offline
                        S Offline
                        silep
                        wrote on last edited by
                        #11

                        The pointer change finished to remove the error. Now I have a violation error when I use my ui, but it seems to be another thing. Thank you very much!

                        1 Reply Last reply
                        0

                        • Login

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