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. How to call an object created in another function.
Forum Updated to NodeBB v4.3 + New Features

How to call an object created in another function.

Scheduled Pinned Locked Moved Solved General and Desktop
c++function
17 Posts 2 Posters 5.6k 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 mrjj
    9 Aug 2016, 07:25

    hi
    Object in other functions are not accessible to others unless used as parameter.
    The trick is to declare the object in the .h file for the class.
    so
    QLabel *lbl1; shoud live in mainwindow.h in the class. (public)

    class MainWindow : public QMainWindow
    {
    Q_OBJECT

    public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
    QLabel *lbl1; //<<<< < this is shared in all functions
    };

    then it all works out for you.

    void createlbl(){
    lbl1 = new QLabel("Hello");
    }

    void Mainwindow :: resizeEvent(QResizeEvent *evt){
    lbl1 -> xxx
    }

    R Offline
    R Offline
    ronyNS
    wrote on 9 Aug 2016, 07:32 last edited by
    #3

    @mrjj
    Thank you so much!

    M 1 Reply Last reply 9 Aug 2016, 07:37
    1
    • R ronyNS
      9 Aug 2016, 07:32

      @mrjj
      Thank you so much!

      M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 9 Aug 2016, 07:37 last edited by
      #4

      @ronyNS
      super :)
      just a note:
      you can get label to fit window automatically- if you use layouts.

      1 Reply Last reply
      2
      • R Offline
        R Offline
        ronyNS
        wrote on 9 Aug 2016, 07:40 last edited by
        #5

        Yes i am aware of that , but this is the problem https://forum.qt.io/topic/69999/how-to-add-a-label-on-a-layout-that-covers-the-entire-window/2
        This is the reason i am using resizeEvent.

        M 1 Reply Last reply 9 Aug 2016, 07:45
        0
        • R ronyNS
          9 Aug 2016, 07:40

          Yes i am aware of that , but this is the problem https://forum.qt.io/topic/69999/how-to-add-a-label-on-a-layout-that-covers-the-entire-window/2
          This is the reason i am using resizeEvent.

          M Offline
          M Offline
          mrjj
          Lifetime Qt Champion
          wrote on 9 Aug 2016, 07:45 last edited by
          #6

          @ronyNS
          ok :)
          Still not sure why layout wont work but resize event will then.
          You just want qLabel to cover all mainwindow ?

          1 Reply Last reply
          2
          • R Offline
            R Offline
            ronyNS
            wrote on 9 Aug 2016, 07:50 last edited by
            #7

            Yes , like i want to cover my login form , but when i drag the label on the ui it resizes according to the layout.
            I used size policy , but when i run it , the main window size is fixed and cannot be reduced.
            If there is an easy way , please tell me :)

            M 1 Reply Last reply 9 Aug 2016, 07:54
            0
            • R ronyNS
              9 Aug 2016, 07:50

              Yes , like i want to cover my login form , but when i drag the label on the ui it resizes according to the layout.
              I used size policy , but when i run it , the main window size is fixed and cannot be reduced.
              If there is an easy way , please tell me :)

              M Offline
              M Offline
              mrjj
              Lifetime Qt Champion
              wrote on 9 Aug 2016, 07:54 last edited by
              #8

              @ronyNS
              ok, sounds like i dont understand the full setup then.
              Maybe resize event will just be easier :)

              1 Reply Last reply
              2
              • R Offline
                R Offline
                ronyNS
                wrote on 9 Aug 2016, 08:00 last edited by
                #9

                Yes , Thank you ! :)

                1 Reply Last reply
                1
                • R Offline
                  R Offline
                  ronyNS
                  wrote on 9 Aug 2016, 14:21 last edited by
                  #10

                  Hey now i am getting 'Expected specifier before Qlabel' error . Please help

                  M 1 Reply Last reply 9 Aug 2016, 14:31
                  0
                  • R ronyNS
                    9 Aug 2016, 14:21

                    Hey now i am getting 'Expected specifier before Qlabel' error . Please help

                    M Offline
                    M Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on 9 Aug 2016, 14:31 last edited by mrjj 8 Sept 2016, 14:32
                    #11

                    @ronyNS said:

                    Expected

                    show line where it says it :)

                    did u include <QLabel>
                    in mainwindow. h?

                    if its for the line
                    QLabel *lbl1;

                    1 Reply Last reply
                    0
                    • R Offline
                      R Offline
                      ronyNS
                      wrote on 9 Aug 2016, 14:50 last edited by
                      #12
                      void createlbl(){
                      lbl1 = new QLabel("Hello");
                      }
                      

                      Error is for this line only when i use lbl1 in other functions. Yes i have included Qlabel in .h file

                      M 1 Reply Last reply 9 Aug 2016, 14:56
                      0
                      • R ronyNS
                        9 Aug 2016, 14:50
                        void createlbl(){
                        lbl1 = new QLabel("Hello");
                        }
                        

                        Error is for this line only when i use lbl1 in other functions. Yes i have included Qlabel in .h file

                        M Offline
                        M Offline
                        mrjj
                        Lifetime Qt Champion
                        wrote on 9 Aug 2016, 14:56 last edited by mrjj 8 Sept 2016, 14:57
                        #13

                        @ronyNS
                        well u are not in mainwindow method..

                        void XXXX::createlbl(){

                        XXXX should be mainwindow

                        when there is no NAME:: in front, its NOT a member function. just a plain function.

                        R 1 Reply Last reply 9 Aug 2016, 15:12
                        0
                        • M mrjj
                          9 Aug 2016, 14:56

                          @ronyNS
                          well u are not in mainwindow method..

                          void XXXX::createlbl(){

                          XXXX should be mainwindow

                          when there is no NAME:: in front, its NOT a member function. just a plain function.

                          R Offline
                          R Offline
                          ronyNS
                          wrote on 9 Aug 2016, 15:12 last edited by
                          #14

                          @mrjj
                          Still the same error

                          void MainWindow:: createlbl(){
                              lbl1 = new Qlabel();
                          }
                          

                          'expected type-specifier before QLabel

                          M 1 Reply Last reply 9 Aug 2016, 15:15
                          0
                          • R ronyNS
                            9 Aug 2016, 15:12

                            @mrjj
                            Still the same error

                            void MainWindow:: createlbl(){
                                lbl1 = new Qlabel();
                            }
                            

                            'expected type-specifier before QLabel

                            M Offline
                            M Offline
                            mrjj
                            Lifetime Qt Champion
                            wrote on 9 Aug 2016, 15:15 last edited by mrjj 8 Sept 2016, 15:18
                            #15

                            but it should work. ?

                            show mainwindow.h

                            here is sample that does so
                            https://www.dropbox.com/s/i0qj2e9mxbggymt/mylabel.zip?dl=0

                            1 Reply Last reply
                            2
                            • R Offline
                              R Offline
                              ronyNS
                              wrote on 9 Aug 2016, 15:57 last edited by
                              #16

                              my mainwindow.h

                              #ifndef MAINWINDOW_H
                              #define MAINWINDOW_H
                              #include<QLabel>
                              #include <QMainWindow>
                              
                              namespace Ui {
                              class MainWindow;
                              }
                              
                              class MainWindow : public QMainWindow
                              {
                                  Q_OBJECT
                              
                              public:
                                  explicit MainWindow(QWidget *parent = 0);
                                  ~MainWindow();
                                  QLabel *lbl;
                              
                              protected:
                                  void resizeEvent(QResizeEvent *evt) override;
                              
                              private:
                                  Ui::MainWindow *ui;
                              };
                              
                              #endif // MAINWINDOW_H
                              

                              my mainwindow.cpp

                              #include "mainwindow.h"
                              #include "ui_mainwindow.h"
                              #include<QLabel>
                              
                              MainWindow::MainWindow(QWidget *parent) :
                                  QMainWindow(parent),
                                  ui(new Ui::MainWindow)
                              {
                                  ui->setupUi(this);
                              }
                              
                              void MainWindow:: createlbl(){
                                  lbl = new Qlabel();
                              }
                              
                              void MainWindow :: resizeEvent(QResizeEvent *evt)
                              {
                                  MainWindow::setCentralWidget(lbl);
                                  lbl->setStyleSheet("Background-color:pink");
                              
                                  QMainWindow::resizeEvent(evt); //call base implementation
                              }
                              
                              MainWindow::~MainWindow()
                              {
                                  delete ui;
                              }
                              
                              
                              
                              1 Reply Last reply
                              0
                              • M Offline
                                M Offline
                                mrjj
                                Lifetime Qt Champion
                                wrote on 9 Aug 2016, 17:33 last edited by
                                #17

                                @ronyNS said:

                                lbl = new Qlabel();

                                misspelled ?
                                its QLabel ?

                                lbl = new QLabel();
                                big L ?

                                1 Reply Last reply
                                2

                                12/17

                                9 Aug 2016, 14:50

                                • Login

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