繁体   English   中英

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

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

我创建了一个horrizontalprogressbar类,在其中创建了一个简单的progressbar。 我还有一个名为mainwindow的类,我想访问和显示mainwindow上horrizontalprogressbar的内容。 我在这里尝试了很多事情,但仍然在不同的窗口上保持水平进度条和主窗口。 无论如何,将它们都显示在同一窗口中。 由于我是QT新手,因此我将为解决此问题提供任何帮助,我们将不胜感激。

请在下面找到代码:-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

水平进度条

#include "horrizontalprogressbar.h"

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

主窗口

#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

主窗口

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

它对我有用,请更改以下内容,以便在构造时将应用新对象的设置:

#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