#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
//1.Combo Box控件
#include<QComboBox>
//2.QFontComboBox控件
#include<QFontComboBox>
#include<QLabel>
//3.Line Edit控件
#include<QLineEdit>
#include <QPushButton>
//4. Plain Text Edit控件
#include <QPlainTextEdit>
#include<QRadioButton>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private:
Ui::MainWindow *ui;
//1.声明一个QComboBox对象
QComboBox *combobox;
private slots:
void comboxIndex(int);
//2.声明QFontComboBox对象
private:
QFontComboBox *fontcombobox;
QLabel *qlabels;
private slots:
void fontcomboboxFunc(QFont);
//3.声明QLineEdit/QPushButton/QLabel对象
private:
QLineEdit *lineedit;
QPushButton *pushbutton;
QLabel *qlabely;
private slots:
void pushbuttonclicked();
//4.QPlainTextEdit/QRadioButton对象
private:
QPlainTextEdit *plaintedit;
QRadioButton *radiobutton;
private slots:
void radioButtonClicked();
};
#endif // MAINWINDOW_H
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMessageBox>
#include <QDebug>
#include <QDir>
#include <QTextStream>//文本流
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
//设置主窗口的显示位置及大小
this->setGeometry(300,200,1000,600);
//1.
combobox = new QComboBox(this);//实例化对象
combobox->setGeometry(10,10,200,30);
combobox->addItem("北京市");
combobox->addItem("上海市");
combobox->addItem("天津市");
combobox->addItem("重庆市");
combobox->addItem("江西市");
combobox->addItem("广东市");
combobox->addItem("香港特别行政区");
combobox->addItem("澳门特别行政区");
//信号槽连接函数实现
connect(combobox,SIGNAL(currentIndexChanged(int)),this,
SLOT(comboxIndex(int)));
//2.
fontcombobox = new QFontComboBox(this);
qlabels =new QLabel(this);
fontcombobox->setGeometry(10,50,200,30);
qlabels->setGeometry(10,80,300,50);
//信号与槽的链接
connect(fontcombobox,SIGNAL(currentFontChanged(QFont)),this,SLOT(fontcomboboxFunc(QFont)));
//3.
lineedit = new QLineEdit(this);
lineedit->setGeometry(10,150,200,30);
pushbutton = new QPushButton(this);
pushbutton->setGeometry(220,150,100,30);
pushbutton->setText("点击我");
qlabely = new QLabel(this);
qlabely->setGeometry(10,200,400,30);
qlabely->setText("你输入的内容为多少:");
connect(pushbutton,SIGNAL(clicked()),this,SLOT(pushbuttonclicked()));
//4.
plaintedit = new QPlainTextEdit(this);
plaintedit->setGeometry(10,240,400,200);
radiobutton = new QRadioButton(this);
radiobutton->setGeometry(260,220,200,30);
radiobutton->setText("只读模式");
//设置当前程序为工作目录
QDir::setCurrent(QCoreApplication::applicationDirPath());
QFile fe("moc_mainwindow.cpp");
fe.open(QFile::ReadOnly | QFile::Text);
//加载到文件流
QTextStream strin(&fe);
plaintedit->insertPlainText(strin.readAll());
//
connect(radiobutton,SIGNAL(clicked()),this,SLOT(radioButtonClicked()));
}
MainWindow::~MainWindow()
{
delete ui;
}
//1.
void MainWindow::comboxIndex(int index)
{
QMessageBox mybox(QMessageBox::Question,"信息",combobox->itemText(index),QMessageBox::Yes | QMessageBox::No);
mybox.exec();
}
//2.
void MainWindow::fontcomboboxFunc(QFont font)
{
qlabels->setFont(font);
QString qStr = "零声教育QT开发工程师";
qlabels->setText(qStr);
}
//3.
void MainWindow::pushbuttonclicked()
{
QString qStr;
qStr = "你输入的内容为:";
qStr = qStr+lineedit->text();
qlabely->setText(qStr);
lineedit->clear();
}
//4.
void MainWindow::radioButtonClicked()
{
//
if(radiobutton->isChecked())
{
plaintedit->setReadOnly(true);
}
else
{
plaintedit->setReadOnly(false);
}
}