-2

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.

Amar Kumar
  • 45
  • 10
  • What problem are you getting when passing the object to a function? can you post some code to clarify the issue? – Mariam Aug 27 '18 at 14:05
  • 1
    Welcome to SO! Please take a moment to read about [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask) and [How to create a Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) – Teasel Aug 27 '18 at 14:32
  • 1
    Apologies, but I am having a rather hard time understanding your question (I tried to edit it, but had to give up when I got totally lost half-way through). Can you rephrase what exactly you want to do and what you are having problems with? – CharonX Aug 27 '18 at 14:44
  • 1
    Please, spent some time to take the [tour](https://stackoverflow.com/tour) as well. – scopchanov Aug 27 '18 at 17:25
  • 1
    The Qt solution is to use signals and slots instead of pass pointers around to widgets. Put a slot on your main window to change the progress value. Emit a signal from your readerfile object that is connected to the slot in you have added to your main window. – drescherjm Aug 27 '18 at 18:50
  • I have added new point . Just check it – Amar Kumar Aug 28 '18 at 05:49

1 Answers1

2

To get a pointer to an object from another class you need to implement a public function that returns this pointer. I will give you a little example:

Your class MainWindow in the header file will include a function progressbar().

mainwindow.h:

//...
class MainWindow : public QMainWindow
{
   Q_ObBJECT
public:
   QProgressBar *progressbar();   //returns a pointer to the QProgressBar
   //..
private:
   //..
};

This function is implemented in mainwindow.cpp like this:

QProgressBar *MainWindow::progressbar()
{
return ui->progbar;    //I just called it like this to avoid confusion, it's the just the name you defined using QtDesigner
}

Then, in demo.hpp if you have an instance of MainWindow in your class:

//..
class Demo : public QObject
{
   Q_OBJECT
public:
   //..
private:
   MainWindow *window;
   //..
}

you can just access QProgressBar using by calling the function in demo.cpp:

QProgressBar *bar;
bar = window->progressbar(); 

I have to say though that it's unusual to have an instance of MainWindow in another class. Usually your QMainWindow or QApplication is the main entry point to the program and you have instances of the other classes in them, not the other way around.

Mariam
  • 342
  • 3
  • 18
  • this one progressbar (ui->progbar) and this one (QProgressBar *bar; bar = window->progressbar(); ) both are same – Amar Kumar Aug 28 '18 at 09:35
  • yes, progbar is in the mainwindow class and bar is the same object but in the demo class, which is what you wanted if I understood correctly. – Mariam Aug 28 '18 at 09:40
  • every calculation I am calculating inside the demo.cpp file. so i just pass the (ui->QprogressBar) over demo.cpp. ui->QprogressBar->setMinimum(0); ui->QprogressBar->setMaximum(100); ui->QprogressBar->setValue(i);, I want to use all the 3 method inside the demo.cpp file not inside the mainwindow.cpp – Amar Kumar Aug 28 '18 at 10:02
  • yes. If you do what I wrote, you can do this by calling bar->setMinimum(0), bar->setValue(i), bar->setMaximum(100). Just try it. – Mariam Aug 28 '18 at 10:13
  • Actually this one getting error for me QProgressBar *bar; bar = window->progressbar(); – Amar Kumar Aug 28 '18 at 10:14
  • I am getting exception violation in this line QProgressBar *MainWindow::progressbar() { return ui->progbar; } – Amar Kumar Aug 28 '18 at 10:25
  • what kind of exception? and access violation? does progbar exist as part of the ui in qt designer? – Mariam Aug 28 '18 at 10:29
  • just see the image view of vilation – Amar Kumar Aug 28 '18 at 10:53
  • you cannot write ui->QProgressBar. This is not correct. As I said either define the progress bar in Designer and give it a name or declare it in the constructor of MainWindow, if you are doing it in code. and the use the name as ui->thenameyouchoseforqprogressbar – Mariam Aug 28 '18 at 11:09