dialog.h文件下写了这个
#ifndef DIALOG_H
#define DIALOG_H
#include <QDialog>
#include <QCheckBox>
#include<QRadioButton>
#include<QPlainTextEdit>
#include<QPushButton>
class Dialog : public QDialog
{
Q_OBJECT
private:
QCheckBox *ChkboxUnder;//下划线
QCheckBox *ChkboxItalic;//斜体
QCheckBox *ChkboxBold;//加粗
QRadioButton *RadioButton_black;//黑色
QRadioButton *RadioButton_blue;//蓝色
QRadioButton *RadioButton_red;//红色
QPlainTextEdit *TextEdit;
QPushButton *btnok;
QPushButton *btncancle;
QPushButton *btnclose;
public:
Dialog(QWidget *parent = nullptr);
~Dialog();
};
#endif // DIALOG_H
dialog.cpp文件下写了这个
#include "dialog.h"
#include<QHBoxLayout>
#include<QVBoxLayout>
Dialog::Dialog(QWidget *parent):QDialog(parent) //.cpp负责实现 .h负责定义:
{
ChkboxUnder=new QCheckBox("下划线");//.h文件已定义类类型,不需要再次定义
ChkboxItalic=new QCheckBox("斜体");
ChkboxBold=new QCheckBox("加粗");//加粗
QHBoxLayout *HLay1=new QHBoxLayout();//我不理解为什么要增加一个这个水平分布的箱子
HLay1->addWidget(ChkboxUnder);
HLay1->addWidget(ChkboxItalic);
HLay1->addWidget(ChkboxBold);
}
Dialog::~Dialog() {}
//结果发现,还是个对话框没法实现
原来是漏了加这个函数
//需要个函数方法实现
setLayout(HLay1);
最重正确方法如下
dialog.h文件入下
#ifndef DIALOG_H
#define DIALOG_H
#include <QDialog>
#include <QCheckBox>
#include<QRadioButton>
#include<QPlainTextEdit>
#include<QPushButton>
class Dialog : public QDialog
{
Q_OBJECT
private:
QCheckBox *ChkboxUnder;//下划线
QCheckBox *ChkboxItalic;//斜体
QCheckBox *ChkboxBold;//加粗
QRadioButton *RadioButton_black;//黑色
QRadioButton *RadioButton_blue;//蓝色
QRadioButton *RadioButton_red;//红色
QPlainTextEdit *TextEdit;
QPushButton *btnok;
QPushButton *btncancle;
QPushButton *btnclose;
public:
Dialog(QWidget *parent = nullptr);
~Dialog();
};
#endif // DIALOG_H
dialog.cpp如下
#include "dialog.h"
#include<QHBoxLayout>
#include<QVBoxLayout>
Dialog::Dialog(QWidget *parent):QDialog(parent) //.cpp负责实现 .h负责定义:
{
ChkboxUnder=new QCheckBox("下划线");//.h文件已定义类类型,不需要再次定义
ChkboxItalic=new QCheckBox("斜体");
ChkboxBold=new QCheckBox("加粗");//加粗
QHBoxLayout *HLay1=new QHBoxLayout();//我不理解为什么要增加一个这个水平分布的箱子
HLay1->addWidget(ChkboxUnder);
HLay1->addWidget(ChkboxItalic);
HLay1->addWidget(ChkboxBold);
//需要个函数方法实现
setLayout(HLay1);
}
Dialog::~Dialog() {}
main.cpp 如下
#include "dialog.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Dialog w;
w.show();
return a.exec();
}
成功运行