簡體   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