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. Why cant I pass the "ui" variable as a pointer?
Forum Updated to NodeBB v4.3 + New Features

Why cant I pass the "ui" variable as a pointer?

Scheduled Pinned Locked Moved General and Desktop
12 Posts 5 Posters 8.5k 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.
  • A Offline
    A Offline
    andreyc
    wrote on last edited by
    #2

    [quote]I Have tried to pass “ui” as a pointer to “StatsObj” but this does not seem to work for some reason.[/quote]
    How exactly it does not work?
    Are there compilation errors?
    Are there run-time exceptions?

    1 Reply Last reply
    0
    • JKSHJ Offline
      JKSHJ Offline
      JKSH
      Moderators
      wrote on last edited by
      #3

      -The variable holds an object in the Ui namespace. That namespace is private, and only visible to your MainWindow class. You cannot pass that variable to other classes.-

      EDIT: Sorry, that was the wrong diagnosis. Please show us the code you tried.

      Anyway, also remember that MainWindow and Ui::MainWindow are two different classes.

      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

      1 Reply Last reply
      0
      • C Offline
        C Offline
        creeveshft
        wrote on last edited by
        #4

        the UI pointer is private. You can make it public and then pass the this pointer to your object.

        1 Reply Last reply
        0
        • JKSHJ Offline
          JKSHJ Offline
          JKSH
          Moderators
          wrote on last edited by
          #5

          [quote author="creeveshft" date="1419411113"]the UI pointer is private. You can make it public and then pass the this pointer to your object.[/quote]No, that's not it. The MainWindow is allowed to pass its private member variables to any function that it calls.

          achmed needs to provide more details (preferably code) about what he has tried.

          Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

          1 Reply Last reply
          0
          • K Offline
            K Offline
            kenchan
            wrote on last edited by
            #6

            Why not just pass the pointer of the MainWindow instead of the ui pointer?

            1 Reply Last reply
            0
            • A Offline
              A Offline
              achmed
              wrote on last edited by
              #7

              Hi. Sorry for replying so late. I was on holiday overseas and only got back to work today.

              I Did not save my previous attempts at trying to pass the ui variable.
              But here is some new code to show what I'm trying to accomplish.
              This example does not compile.

              Remember: My main goal is to be able to access the ui varaible from sub classes. Maybe there is an easier more code efficient way that I'm not thinking of right now.

              @#ifndef MAINWINDOW_H
              #define MAINWINDOW_H

              #include <QMainWindow>
              #include "myclass.h"

              namespace Ui {
              class MainWindow;
              }

              class MainWindow : public QMainWindow
              {
              Q_OBJECT

              public:
              MyClass *MyObject;

              explicit MainWindow(QWidget *parent = 0);
              ~MainWindow();
              

              private:
              Ui::MainWindow *ui;
              };

              #endif // MAINWINDOW_H
              @

              @#include "mainwindow.h"
              #include "ui_mainwindow.h"

              MainWindow::MainWindow(QWidget *parent) :
              QMainWindow(parent),
              ui(new Ui::MainWindow)
              {
              ui->setupUi(this);
              MyObject = new MyClass(this,ui);
              }

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

              @#ifndef MYCLASS_H
              #define MYCLASS_H

              #include <QObject>
              #include <QMainWindow>

              namespace Ui {
              class MainWindow;
              }

              class MyClass : public QObject
              {
              Q_OBJECT
              public:
              explicit MyClass(QObject *parent = 0, Ui::MainWindow *ui_pointer = 0);

              signals:

              public slots:

              };

              #endif // MYCLASS_H
              @

              @#include "myclass.h"

              MyClass::MyClass(QObject *parent, Ui::MainWindow *ui_pointer) :
              QObject(parent)
              {
              ui_pointer->QLabel->SetText("abc");
              }
              @

              1 Reply Last reply
              0
              • JKSHJ Offline
                JKSHJ Offline
                JKSH
                Moderators
                wrote on last edited by
                #8

                Hi,

                Please describe exactly what doesn't work, and what error messages you get.

                Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                1 Reply Last reply
                0
                • A Offline
                  A Offline
                  achmed
                  wrote on last edited by
                  #9

                  Ok. Thanks for your patience. The code does not compile.
                  Here are the errors I get:

                  @X:\MTEK\Programming\Delete\PassUi2\PassUi\myclass.cpp:-1: In constructor 'MyClass::MyClass(QObject*, Ui::MainWindow*)':

                  X:\MTEK\Programming\Delete\PassUi2\PassUi\myclass.cpp:6: error: invalid use of incomplete type 'class Ui::MainWindow'
                  ui_pointer->QLabel->SetText("abc");
                  ^

                  X:\MTEK\Programming\Delete\PassUi2\PassUi\myclass.cpp:1: In file included from ..\PassUi\myclass.cpp:1:0:

                  X:\MTEK\Programming\Delete\PassUi2\PassUi\myclass.h:8: error: forward declaration of 'class Ui::MainWindow'
                  class MainWindow;
                  ^@

                  1 Reply Last reply
                  0
                  • K Offline
                    K Offline
                    kenchan
                    wrote on last edited by
                    #10

                    Missing #include "ui_mainwindow.h" in your MyClass file perhaps?

                    1 Reply Last reply
                    0
                    • JKSHJ Offline
                      JKSHJ Offline
                      JKSH
                      Moderators
                      wrote on last edited by
                      #11

                      [quote]invalid use of incomplete type 'class Ui::MainWindow'[/quote]That means you didn't include the header which defines the Ui::MainWindow class structure. You need #include "ui_mainwindow.h"

                      This is a common error message in C++, so remember it well.

                      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                      1 Reply Last reply
                      0
                      • A Offline
                        A Offline
                        achmed
                        wrote on last edited by
                        #12

                        Ok. Thank you very much. Problem solved.

                        1 Reply Last reply
                        0
                        • A Anonymous_Banned275 referenced this topic on

                        • Login

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