0

I have created a class horrizontalprogressbar in which i created a simple progressbar. I have another class called mainwindow and I would like to access and display the contents of horrizontalprogressbar on mainwindow. I tried a lot off things here, but still I keep getting the horrizontalprogressbar and mainwindow on separate windows. Is there anyway to display them both on the same same window. Since I am new to QT, I would much appreciate any help I could get to resolve this.

Please find the code below:- horrizontalprogressbar.h

#ifndef HORRIZONTALPROGRESSBAR_H
#define HORRIZONTALPROGRESSBAR_H

#include <QProgressBar>
#include <QWidget>

class horrizontalprogressbar: public QProgressBar
{
    Q_OBJECT
public:
    horrizontalprogressbar();
    QProgressBar progressBar_horizontal;
};

#endif // HORRIZONTALPROGRESSBAR_H

horrizontalprogressbar.cpp

#include "horrizontalprogressbar.h"

horrizontalprogressbar::horrizontalprogressbar()
{
    progressBar_horizontal.setRange(0,5);
    progressBar_horizontal.setValue(2.5);
    progressBar_horizontal.setFixedSize(300,50);
    //progressBar_horizontal.show();
}

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QtWidgets>

class horrizontalprogressbar;

class MainWindow : public QMainWindow
{
    Q_OBJECT

private:
    horrizontalprogressbar *progressbar_H;

public:
    MainWindow();//(QWidget *parent = 0);
    ~MainWindow();
};

#endif // MAINWINDOW_H

mainwindow.cpp

#include "mainwindow.h"
#include "horrizontalprogressbar.h"

MainWindow::MainWindow()//(QWidget *parent)
   // : QMainWindow(parent)
{
    progressbar_H = new horrizontalprogressbar;
    setCentralWidget(progressbar_H);
    progressbar_H->setParent(this);
    //progressbar_H->setFixedSize(200,200);
    //progressbar_H->show();

}

MainWindow::~MainWindow()
{

}

main.cpp

#include "mainwindow.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.setFixedSize(800,600);
    w.setStyleSheet("QMainWindow {background: 'black';}");
    w.show();

    return a.exec();
}
Sam
  • 113
  • 5
  • 13

1 Answers1

0

It works for me, change the following so that at construction the settings of your new object will be applied:

#include "horrizontalprogressbar.h"

horrizontalprogressbar::horrizontalprogressbar()
{
   this->setRange(0,5);
   this->setValue(2.5);
   this->setFixedSize(300,50);
   //    progressBar_horizontal.show();
}
JZH
  • 1
  • 5
  • And of course by this you can delete the member QProgressBar progressBar_horizontal; from your header horrizontalprogressbar.h, because we don't need it anymore – JZH Mar 16 '16 at 21:38
  • Thanks a lots JZK, I just tried it and it works for me. Much obliged. – Sam Mar 16 '16 at 22:02
  • Can we add the horrizontalprogressbar in a QGroupBox and display on mainwindow?. How can it be done? – Sam Mar 17 '16 at 16:17