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]Member variables don't seem to exist inside class
Forum Updated to NodeBB v4.3 + New Features

[Solved]Member variables don't seem to exist inside class

Scheduled Pinned Locked Moved General and Desktop
11 Posts 4 Posters 4.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.
  • M Offline
    M Offline
    mrplainswalker
    wrote on last edited by
    #1

    I'm trying a access a pointer inside one of my class' functions. This variable was initialized in the constructor (it happens to be a pointer to a widget that gets created with new), so I know it's there. However, when I try to access it from another function, I get a seg fault. When I run the debugger and look under the 'this' pointer, it says "unavailable synchronous data". Any member variable should be available in any of that class' functions. What's going on here?

    1 Reply Last reply
    0
    • K Offline
      K Offline
      koahnig
      wrote on last edited by
      #2

      Well, with the information you provide, probably nobody but yourself will be able to provide assistance.

      Maybe you have overwritten the pointer either on purpose or by accident.

      If you can provide some short example code showing the problem, you will give others the chance to help you.

      Vote the answer(s) that helped you to solve your issue(s)

      1 Reply Last reply
      0
      • M Offline
        M Offline
        mrplainswalker
        wrote on last edited by
        #3

        Well, the variable isn't being overwritten. It simply no longer exists.

        In the constructor, I have:

        @MainWindow::MainWindow()
        {
        //create the central area and add it to the main window
        m_CentralArea = new QMdiArea;
        setCentralWidget(m_CentralArea);

        //create the notebook and add it to the dock area
        m_Notebook = new Notebook;
        QDockWidget *dockWidget = new QDockWidget(tr("Notebook"), this);
        dockWidget->setAllowedAreas(Qt::LeftDockWidgetArea |
                                    Qt::RightDockWidgetArea |
                                    Qt::TopDockWidgetArea |
                                    Qt::BottomDockWidgetArea);
        dockWidget->setWidget(m_Notebook);
        addDockWidget(Qt::LeftDockWidgetArea, dockWidget);
        m_Notebook->show();
        

        }@

        Then, later in another function:

        @void MainWindow::addNewNote()
        {
        NewNoteWindow *newNote = new NewNoteWindow(m_Notebook);
        m_CentralArea->addSubWindow(newNote);
        }@

        At this point, m_Notebook no longer exists and I get a seg fault.

        1 Reply Last reply
        0
        • K Offline
          K Offline
          koahnig
          wrote on last edited by
          #4

          Please post also the associated header file and other code which might be relevant for your case.

          Vote the answer(s) that helped you to solve your issue(s)

          1 Reply Last reply
          0
          • K Offline
            K Offline
            koahnig
            wrote on last edited by
            #5

            One thing came to my mind when looking in more detail to the supplied code.
            I guess Qt typically takes over the responsibilities of memory you supply with pointers. Possibly it is internally moved.

            Check if it is still in its location after you have called setWidget and addDockWidget in your constructor.

            Vote the answer(s) that helped you to solve your issue(s)

            1 Reply Last reply
            0
            • M Offline
              M Offline
              mrplainswalker
              wrote on last edited by
              #6

              Here's what my header file looks like

              @#ifndef MAINWINDOW_H
              #define MAINWINDOW_H

              #include <QMainWindow>
              #include <QtGui>

              #include "notebook.h"

              class Notebook;

              namespace Ui {
              class MainWindow;
              }

              class MainWindow : public QMainWindow
              {
              Q_OBJECT

              public:
              MainWindow();
              ~MainWindow();

              void addNewNote();
              

              private:
              Ui::MainWindow *ui;
              QMdiArea *m_CentralArea;
              Notebook *m_Notebook;
              };

              #endif // MAINWINDOW_H@

              I can't think of any other code that would be relevant. One other interesting thing to note is that I couldn't build until I forward declared the Notebook class at the beginning of the header file. Otherwise it wasn't recognizing Notebook as a type, even though I was including notebook.h. I'm not sure if that's important, but it might be.

              I ran the debugger and m_Notebook is accessible all the way through the end of the constructor, so setWidget and addDockWidget don't seem to be the culprits.

              1 Reply Last reply
              0
              • G Offline
                G Offline
                giesbert
                wrote on last edited by
                #7

                Hi,

                can you post a full, compilable example?
                I think, there is a bug in notebook.h, otherwise the type would be known.
                Second, it should live, until you don't delete the dock.

                Did you try to use QPointer<Notebook> which will be set to 0 if the object is destroyed?

                Nokia Certified Qt Specialist.
                Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

                1 Reply Last reply
                0
                • K Offline
                  K Offline
                  koahnig
                  wrote on last edited by
                  #8

                  [quote author="mrplainswalker" date="1313163508"]One other interesting thing to note is that I couldn't build until I forward declared the Notebook class at the beginning of the header file. Otherwise it wasn't recognizing Notebook as a type, even though I was including notebook.h. I'm not sure if that's important, but it might be.
                  [/quote]

                  Please post notebook.h

                  Which OS and compiler are you using?

                  Vote the answer(s) that helped you to solve your issue(s)

                  1 Reply Last reply
                  0
                  • M Offline
                    M Offline
                    mrplainswalker
                    wrote on last edited by
                    #9

                    First off, I'm using Windows 7, Qt 4.7.3 and mingw.

                    I think I found the problem. The problem is that my parent pointer in my notebook class was incorrect. It was pointing at something other than a mainwindow object.

                    However, I'm unclear about how to solve the problem. Because Qt manages pointers by itself once you hand over control to something like a QMdiArea, I can't count on the pointers always remaining the same. However, I need to know what the pointers are in order to be able to communicate between classes.

                    I have a main window with many subwindows that come in and out of existence. The subwindows need to be able to pass objects back and forth between each other. What is the best way to manage that? Every method seems to require the objects to have pointers to each other.

                    1 Reply Last reply
                    0
                    • M Offline
                      M Offline
                      mlong
                      wrote on last edited by
                      #10

                      [quote author="mrplainswalker" date="1313166857"]However, I need to know what the pointers are in order to be able to communicate between classes.

                      I have a main window with many subwindows that come in and out of existence. The subwindows need to be able to pass objects back and forth between each other. What is the best way to manage that? Every method seems to require the objects to have pointers to each other.[/quote]

                      Sounds like it might be a problem that signals and slots could solve.

                      Software Engineer
                      My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

                      1 Reply Last reply
                      0
                      • M Offline
                        M Offline
                        mrplainswalker
                        wrote on last edited by
                        #11

                        Well, after tinkering with it, I was able to get signals and slots to essentially do what I want. I'll mark this as solved.

                        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