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. qt access ui elements from another class?
Forum Updated to NodeBB v4.3 + New Features

qt access ui elements from another class?

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 3 Posters 1.1k 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
    amarism
    wrote on last edited by
    #1

    I have a progress bar object (ui->QprogressBar) available in my mainwindow.cpp. But, I want to use this object in another class (readerfile.cpp).

    Headers
    mainwindow.h
    demo.h

    Sources
    mainwindow.cpp
    demo.cpp

    I use this method to call object most of the time:- Using a function call, for example -mainwindow.cpp I will call this function

    mainwindow->isFunction(ui->QprogressBar);
    

    isFunction is available inside my demo.cpp file

    void demo :: isfunction (QProgressBar *progress)
    

    But, Now I want to use QprogressBar object directly inside my demo.cpp file. I tried all possible combinations, connections and just can't get it work. So could someone please explain me, how to access UI elements object from class demo.

    Any idea for the solution will be a great help. Thanks.

    VRoninV Pablo J. RoginaP 2 Replies Last reply
    0
    • A amarism

      I have a progress bar object (ui->QprogressBar) available in my mainwindow.cpp. But, I want to use this object in another class (readerfile.cpp).

      Headers
      mainwindow.h
      demo.h

      Sources
      mainwindow.cpp
      demo.cpp

      I use this method to call object most of the time:- Using a function call, for example -mainwindow.cpp I will call this function

      mainwindow->isFunction(ui->QprogressBar);
      

      isFunction is available inside my demo.cpp file

      void demo :: isfunction (QProgressBar *progress)
      

      But, Now I want to use QprogressBar object directly inside my demo.cpp file. I tried all possible combinations, connections and just can't get it work. So could someone please explain me, how to access UI elements object from class demo.

      Any idea for the solution will be a great help. Thanks.

      VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #2

      @amarism said in qt access ui elements from another class?:

      Now I want to use QprogressBar object directly inside my demo.cpp file.

      No, you don't. That's bad design. Your object in demo.h/cpp should send a signal to notify the progress change but it's the responsibility of the object in mainwindow.h/cpp to pick up that signal and use it to update the progressbar

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      1 Reply Last reply
      4
      • A amarism

        I have a progress bar object (ui->QprogressBar) available in my mainwindow.cpp. But, I want to use this object in another class (readerfile.cpp).

        Headers
        mainwindow.h
        demo.h

        Sources
        mainwindow.cpp
        demo.cpp

        I use this method to call object most of the time:- Using a function call, for example -mainwindow.cpp I will call this function

        mainwindow->isFunction(ui->QprogressBar);
        

        isFunction is available inside my demo.cpp file

        void demo :: isfunction (QProgressBar *progress)
        

        But, Now I want to use QprogressBar object directly inside my demo.cpp file. I tried all possible combinations, connections and just can't get it work. So could someone please explain me, how to access UI elements object from class demo.

        Any idea for the solution will be a great help. Thanks.

        Pablo J. RoginaP Offline
        Pablo J. RoginaP Offline
        Pablo J. Rogina
        wrote on last edited by
        #3

        @amarism said in qt access ui elements from another class?:

        I tried all possible combinations, connections

        Could post any of them?

        @VRonin is right, letting your Demo class know about specifics of your UI is a bad design idea. Let's say that next month you change the QProgressBar into a LCD display to show progress, you'll need to change Demo class as well.

        Ideally, your Demo class should have a signal progressUpdated(int) that you'll emit whenever you know some progress have occurred. Then you connect such signal with a slot in your MainWindow class and act accordingly. Pseudo-code:

        void Demo::superDuperAction()
        {
            // lots of things to do, that will progress over time
            progress++;
            ...
            emit progressUpdated(progress);
        }
        

        mainwindow.h

        class MainWindow :
            ...
            slots:
            updateProgress(int);
        
        private:
             Demo demo;
             ....
        

        mainwindow.cpp

        ...
        void doSomething() {
            demo.doSomeActionThatNeedsShowingProgress();
            ...
        }
        
        ...
        
        void MainWindows:updateProgress(int progress) {
            ui->progressBar->update(progress);
        }
        

        Upvote the answer(s) that helped you solve the issue
        Use "Topic Tools" button to mark your post as Solved
        Add screenshots via postimage.org
        Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

        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