如何在QT中将小部件的内容添加和显示到另一个小部件上
How to add and show the contents of a widget onto another widget in QT?
我创建了一个类 horrizontalprogressbar,在其中我创建了一个简单的进度条。我有另一个叫做主窗口的类,我想在主窗口上访问和显示 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
恐怖进步吧.cpp
#include "horrizontalprogressbar.h"
horrizontalprogressbar::horrizontalprogressbar()
{
progressBar_horizontal.setRange(0,5);
progressBar_horizontal.setValue(2.5);
progressBar_horizontal.setFixedSize(300,50);
//progressBar_horizontal.show();
}
主窗口.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
主窗口.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()
{
}
主.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();
}
相关文章:
- Qt:无法直接为带有子项的小部件添加滚动条
- 在QT中为小部件添加彩色边框的最佳方法是什么
- 将小部件添加到布局后,QStylesheet 不起作用
- C++ Qt如何在滚动区域添加小部件?
- 如何从一个向量中删除最小元素并添加到另一个向量,而第一个变为空?
- 将STL文件添加到QT小部件
- 如何使用Qt中的代码将小部件添加到中央小部件中
- 如何将开关QML添加到QT小部件
- 递归地将 1 添加到 BST 中的所有节点,但具有最小数据的节点除外
- 向 Q 小部件添加滚动条
- 如何将小部件添加到qt工具栏
- C++ 创建小部件数组
- 动态地将自定义 qt 小部件添加到框架中,而无需每次都更改布局
- Qt - 无效使用不完整的类型 'class QScrollBar' - 为文本编辑小部件添加水平滚动条
- 在Qt中向小部件添加滚动条的最快和最佳方法
- 如何将自定义小部件添加为工具按钮的弹出菜单
- 将自定义小部件添加为 QTableWidget HorizontalHeader
- Gtk Notebook小部件添加新选项卡错误
- 在QT c++中为包含布局的小部件添加滚动条
- 向小部件添加标签