简体   繁体   中英

How to add and show the contents of a widget onto another widget in QT?

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();
}

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();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM