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.

How to call an object created in another function.

Scheduled Pinned Locked Moved Solved General and Desktop
c++function
17 Posts 2 Posters 5.4k Views
  • 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.
  • ronyNSR Offline
    ronyNSR Offline
    ronyNS
    wrote on last edited by ronyNS
    #1

    Hello

    I want to call an object of class QLabel in another function . I know this is basic C++ ,but its confusing in Qt .

    in my mainwindow.cpp i have created a function

    void createlbl(){
    
    QLabel *lbl1 = new QLabel("Hello");
    
    }
    
    void Mainwindow :: resizeEvent(QResizeEvent *evt){
    
    
    }
    

    i need to call the label object (lbl1) in the resizeEvent function to resize it.
    I also need to call the object in other functions in my program.
    I tried different ways but i am not getting the right way to call it and use in different functions.
    Please tell me what parameters should i pass and how i should call the class or the function in which the class is created to use in different functions.

    Thank you

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #2

      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
      }

      ronyNSR 1 Reply Last reply
      3
      • mrjjM mrjj

        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
        }

        ronyNSR Offline
        ronyNSR Offline
        ronyNS
        wrote on last edited by
        #3

        @mrjj
        Thank you so much!

        mrjjM 1 Reply Last reply
        1
        • ronyNSR ronyNS

          @mrjj
          Thank you so much!

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on 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
          • ronyNSR Offline
            ronyNSR Offline
            ronyNS
            wrote on 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.

            mrjjM 1 Reply Last reply
            0
            • ronyNSR ronyNS

              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.

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on 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
              • ronyNSR Offline
                ronyNSR Offline
                ronyNS
                wrote on 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 :)

                mrjjM 1 Reply Last reply
                0
                • ronyNSR ronyNS

                  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 :)

                  mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on 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
                  • ronyNSR Offline
                    ronyNSR Offline
                    ronyNS
                    wrote on last edited by
                    #9

                    Yes , Thank you ! :)

                    1 Reply Last reply
                    1
                    • ronyNSR Offline
                      ronyNSR Offline
                      ronyNS
                      wrote on last edited by
                      #10

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

                      mrjjM 1 Reply Last reply
                      0
                      • ronyNSR ronyNS

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

                        mrjjM Offline
                        mrjjM Offline
                        mrjj
                        Lifetime Qt Champion
                        wrote on last edited by mrjj
                        #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
                        • ronyNSR Offline
                          ronyNSR Offline
                          ronyNS
                          wrote on 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

                          mrjjM 1 Reply Last reply
                          0
                          • ronyNSR ronyNS
                            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

                            mrjjM Offline
                            mrjjM Offline
                            mrjj
                            Lifetime Qt Champion
                            wrote on last edited by mrjj
                            #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.

                            ronyNSR 1 Reply Last reply
                            0
                            • mrjjM mrjj

                              @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.

                              ronyNSR Offline
                              ronyNSR Offline
                              ronyNS
                              wrote on last edited by
                              #14

                              @mrjj
                              Still the same error

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

                              'expected type-specifier before QLabel

                              mrjjM 1 Reply Last reply
                              0
                              • ronyNSR ronyNS

                                @mrjj
                                Still the same error

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

                                'expected type-specifier before QLabel

                                mrjjM Offline
                                mrjjM Offline
                                mrjj
                                Lifetime Qt Champion
                                wrote on last edited by mrjj
                                #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
                                • ronyNSR Offline
                                  ronyNSR Offline
                                  ronyNS
                                  wrote on 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
                                  • mrjjM Offline
                                    mrjjM Offline
                                    mrjj
                                    Lifetime Qt Champion
                                    wrote on last edited by
                                    #17

                                    @ronyNS said:

                                    lbl = new Qlabel();

                                    misspelled ?
                                    its QLabel ?

                                    lbl = new QLabel();
                                    big L ?

                                    1 Reply Last reply
                                    2

                                    • Login

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