简体   繁体   English

如何在QT中将小部件的内容添加并显示到另一个小部件上?

[英]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. 我创建了一个horrizontalprogressbar类,在其中创建了一个简单的progressbar。 I have another class called mainwindow and I would like to access and display the contents of horrizontalprogressbar on mainwindow. 我还有一个名为mainwindow的类,我想访问和显示mainwindow上horrizontalprogressbar的内容。 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. 由于我是QT新手,因此我将为解决此问题提供任何帮助,我们将不胜感激。

Please find the code below:- horrizontalprogressbar.h 请在下面找到代码:-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 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();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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