QT将基类从QDialog直接提升为QWidget窗口

前言:有时候建立新工程的时候把窗口的基类设置为了QDialog,比如说我,因为开始入门的时候一直是继承QDialog,所以后面也没有再注意到这个问题,后面发现QDialog实现的窗口不能实现缩放,所以后来就造成了现在的困扰,想要将其提升为QWidget窗口(不用移植到新工程)。

QDialog就是一个简单的消息框,和QMainwindow一样继承来自QWidget,先来看看三者之间的区别。
首先可以看到在这里有三个基类可以选择,分别是:QDialog QMainwindow QWidget

在这里插入图片描述
QDialog 界面
在这里插入图片描述
QMainwindow 界面
在这里插入图片描述
QWidget界面
在这里插入图片描述
下面就讲一下如何将窗口直接从从QDialog直接提升为QWidget窗口,只需要简单的几步:

环境: QT5.7 MSVC2013 WIN10

1、将初始化时定义的基类从QDialog变为QWidget,头文件和cpp文件都需改动,头文件中还需要添加QWidget的头文件
2、修改UI界面的头文件ui_dialog.h(或者其他的名称),将其中的QDialog变为QWidget,应该有两处
3、修改UI界面 .ui文件,用常用的文本编辑器打开就行,比如sublinme打开,该文件不能再QT creater 中修改,所以用其他方式打开,将其中的class=‘QDialog’ 修改 为class=’QWidget‘,此处如不修改的话,UI界面就还是默认使用’QDialog’ ,那么第二步中做的就白费了,还会被重新修改回来。
  • 0
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的实现: ``` #include <QDialog> #include <QLabel> #include <QPushButton> #include <QHBoxLayout> #include <QVBoxLayout> #include <QDir> #include <QDebug> class ImageViewer : public QDialog { Q_OBJECT public: ImageViewer(QWidget *parent = nullptr); private slots: void onPreviousClicked(); void onNextClicked(); private: QLabel *m_imageLabel; QPushButton *m_previousButton; QPushButton *m_nextButton; QStringList m_imageList; int m_currentIndex; }; ImageViewer::ImageViewer(QWidget *parent) : QDialog(parent) { m_imageLabel = new QLabel(this); m_previousButton = new QPushButton(tr("上一张"), this); m_nextButton = new QPushButton(tr("下一张"), this); QHBoxLayout *buttonLayout = new QHBoxLayout; buttonLayout->addWidget(m_previousButton); buttonLayout->addWidget(m_nextButton); QVBoxLayout *mainLayout = new QVBoxLayout; mainLayout->addWidget(m_imageLabel); mainLayout->addLayout(buttonLayout); setLayout(mainLayout); // 查找图片文件 QDir imagesDir(QCoreApplication::applicationDirPath() + "/images"); QStringList filters; filters << "*.jpg" << "*.jpeg" << "*.png" << "*.bmp"; m_imageList = imagesDir.entryList(filters, QDir::Files | QDir::NoSymLinks); m_currentIndex = 0; if (m_imageList.size() > 0) { QString imagePath = imagesDir.absoluteFilePath(m_imageList.at(m_currentIndex)); m_imageLabel->setPixmap(QPixmap(imagePath)); m_imageLabel->setScaledContents(true); } connect(m_previousButton, &QPushButton::clicked, this, &ImageViewer::onPreviousClicked); connect(m_nextButton, &QPushButton::clicked, this, &ImageViewer::onNextClicked); } void ImageViewer::onPreviousClicked() { if (m_imageList.size() > 0) { m_currentIndex--; if (m_currentIndex < 0) { m_currentIndex = m_imageList.size() - 1; } QString imagePath = QDir::currentPath() + "/images/" + m_imageList.at(m_currentIndex); m_imageLabel->setPixmap(QPixmap(imagePath)); } } void ImageViewer::onNextClicked() { if (m_imageList.size() > 0) { m_currentIndex++; if (m_currentIndex >= m_imageList.size()) { m_currentIndex = 0; } QString imagePath = QDir::currentPath() + "/images/" + m_imageList.at(m_currentIndex); m_imageLabel->setPixmap(QPixmap(imagePath)); } } ``` 在构造函数中,我们首先查找指定目录下所有符合条件的图片文件,然后将第一张图片显示在标签上。之后,我们连接“上一张”和“下一张”按钮的点击事件到对应的槽函数。在槽函数中,我们更新当前图片的索引值,然后根据索引值找到对应的图片文件,并将其显示在标签上。需要注意的是,当索引值越界时,我们需要将其重置到合法范围内。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值