qt5系列文章之初识qt
提示:这里可以添加系列文章的所有文章的目录,目录需要自己手动添加
例如:第一章 Python 机器学习入门之pandas的使用
提示:写完文章后,目录可以自动生成,如何生成可参考右边的帮助文档
前言
提示:这里可以添加本文要记录的大概内容:
例如:随着人工智能的不断发展,机器学习这门技术也越来越重要,很多人都开启了学习机器学习,本文就介绍了机器学习的基础内容。
提示:以下是本篇文章正文内容,下面案例可供参考
一、qt是什么?
qt是一套 程序 开发 类库。使用qt c++写程序,程序可以跨平台移植(再编译),可以利用各种开源的类库。
二、qt的获取和安装
开源,
2017年,qt5.9.0,长期支持版本(long term surpported)。
当前学习的是桌面程序开发版,安装包只有一个可执行文件,按照向导安装。
三.qt creator初步使用
打开qt creator,在tools处将语言设置为english
四.编写一个简单程序
1.新建一个项目
实现对输入文本字体增加下划线、斜体、变粗、改变字体颜色的功能。
//main.cpp
#include "dialog.h"
#include <QApplication>
//实现为输入文本加下划线、斜体、变粗,以及改变输入文本字体颜色的功能
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Dialog w;
w.show();
return a.exec();
}
//dialog.cpp
#include "dialog.h"
#include "ui_dialog.h"
Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
ui->setupUi(this);
connect(ui->radioButtonBlack,SIGNAL(clicked()),this,SLOT(setTextFontColor()));
connect(ui->radioButtonBlue,SIGNAL(clicked()),this,SLOT(setTextFontColor()));
connect(ui->radioButtonRed,SIGNAL(clicked()),this,SLOT(setTextFontColor()));
}
Dialog::~Dialog()
{
delete ui;
}
void Dialog::on_checkBoxUder_clicked(bool checked)
{
//设置下划线,对文本框文字增加下划线
QFont font=ui->plainTextEdit->font();
font.setUnderline(checked);
ui->plainTextEdit->setFont(font);
}
void Dialog::on_checkBoxItalic_clicked(bool checked)
{
//设置斜体,文本变斜
QFont font=ui->plainTextEdit->font();
font.setItalic(checked);
ui->plainTextEdit->setFont(font);
}
void Dialog::on_checkBoxBold_clicked(bool checked)
{
//设置粗体,文本变粗
QFont font=ui->plainTextEdit->font();
font.setBold(checked);
ui->plainTextEdit->setFont(font); //最终设置字体颜色
}
void Dialog::setTextFontColor() //自定义槽,需要在构造函数中手动关联
{
//设置文本字体颜色
QPalette plet=ui->plainTextEdit->palette();
//判断要设置的文本是什么颜色,依据是哪个颜色按钮被按到了
if(ui->radioButtonBlack->isChecked())
plet.setColor(QPalette::Text,Qt::black); //设置文本类型是Text,设置颜色是黑色
else if(ui->radioButtonBlue->isChecked())
plet.setColor(QPalette::Text,Qt::blue); //设置文本类型是Text,设置颜色是蓝色
else if(ui->radioButtonRed->isChecked())
plet.setColor(QPalette::Text,Qt::red); //设置文本类型是Text,设置颜色是红色
else
plet.setColor(QPalette::Text,Qt::black); //除此之外,默认设置文本类型是Text,设置颜色是黑色
ui->plainTextEdit->setPalette(plet); //最终设置调色板
}
//dialog.h
#ifndef DIALOG_H
#define DIALOG_H
#include <QDialog>
namespace Ui {
class Dialog;
}
class Dialog : public QDialog
{
Q_OBJECT
public:
explicit Dialog(QWidget *parent = 0);
~Dialog();
private slots:
void on_checkBoxUder_clicked(bool checked); //下划线
void on_checkBoxItalic_clicked(bool checked); //斜体
void on_checkBoxBold_clicked(bool checked); //粗体
void setTextFontColor(); //字体颜色
private:
Ui::Dialog *ui;
};
#endif // DIALOG_H
//ui_dialog.h
/********************************************************************************
** Form generated from reading UI file 'dialog.ui'
**
** Created by: Qt User Interface Compiler version 5.9.0
**
** WARNING! All changes made in this file will be lost when recompiling UI file!
********************************************************************************/
#ifndef UI_DIALOG_H
#define UI_DIALOG_H
#include <QtCore/QVariant>
#include <QtWidgets/QAction>
#include <QtWidgets/QApplication>
#include <QtWidgets/QButtonGroup>
#include <QtWidgets/QCheckBox>
#include <QtWidgets/QDialog>
#include <QtWidgets/QGroupBox>
#include <QtWidgets/QHBoxLayout>
#include <QtWidgets/QHeaderView>
#include <QtWidgets/QPlainTextEdit>
#include <QtWidgets/QPushButton>
#include <QtWidgets/QRadioButton>
#include <QtWidgets/QSpacerItem>
#include <QtWidgets/QVBoxLayout>
QT_BEGIN_NAMESPACE
class Ui_Dialog
{
public:
QVBoxLayout *verticalLayout;
QGroupBox *groupBox;
QHBoxLayout *horizontalLayout_2;
QCheckBox *checkBoxUder;
QCheckBox *checkBoxItalic;
QCheckBox *checkBoxBold;
QGroupBox *groupBox_2;
QHBoxLayout *horizontalLayout_3;
QRadioButton *radioButtonBlack;
QRadioButton *radioButtonRed;
QRadioButton *radioButtonBlue;
QPlainTextEdit *plainTextEdit;
QHBoxLayout *horizontalLayout;
QPushButton *pushButtonClose;
QSpacerItem *horizontalSpacer;
QPushButton *pushButtonCancel;
QSpacerItem *horizontalSpacer_2;
QPushButton *pushButtonOK;
void setupUi(QDialog *Dialog)
{
if (Dialog->objectName().isEmpty())
Dialog->setObjectName(QStringLiteral("Dialog"));
Dialog->resize(699, 565);
verticalLayout = new QVBoxLayout(Dialog);
verticalLayout->setSpacing(6);
verticalLayout->setContentsMargins(11, 11, 11, 11);
verticalLayout->setObjectName(QStringLiteral("verticalLayout"));
groupBox = new QGroupBox(Dialog);
groupBox->setObjectName(QStringLiteral("groupBox"));
horizontalLayout_2 = new QHBoxLayout(groupBox);
horizontalLayout_2->setSpacing(6);
horizontalLayout_2->setContentsMargins(11, 11, 11, 11);
horizontalLayout_2->setObjectName(QStringLiteral("horizontalLayout_2"));
checkBoxUder = new QCheckBox(groupBox);
checkBoxUder->setObjectName(QStringLiteral("checkBoxUder"));
horizontalLayout_2->addWidget(checkBoxUder);
checkBoxItalic = new QCheckBox(groupBox);
checkBoxItalic->setObjectName(QStringLiteral("checkBoxItalic"));
horizontalLayout_2->addWidget(checkBoxItalic);
checkBoxBold = new QCheckBox(groupBox);
checkBoxBold->setObjectName(QStringLiteral("checkBoxBold"));
horizontalLayout_2->addWidget(checkBoxBold);
verticalLayout->addWidget(groupBox);
groupBox_2 = new QGroupBox(Dialog);
groupBox_2->setObjectName(QStringLiteral("groupBox_2"));
horizontalLayout_3 = new QHBoxLayout(groupBox_2);
horizontalLayout_3->setSpacing(6);
horizontalLayout_3->setContentsMargins(11, 11, 11, 11);
horizontalLayout_3->setObjectName(QStringLiteral("horizontalLayout_3"));
radioButtonBlack = new QRadioButton(groupBox_2);
radioButtonBlack->setObjectName(QStringLiteral("radioButtonBlack"));
horizontalLayout_3->addWidget(radioButtonBlack);
radioButtonRed = new QRadioButton(groupBox_2);
radioButtonRed->setObjectName(QStringLiteral("radioButtonRed"));
horizontalLayout_3->addWidget(radioButtonRed);
radioButtonBlue = new QRadioButton(groupBox_2);
radioButtonBlue->setObjectName(QStringLiteral("radioButtonBlue"));
horizontalLayout_3->addWidget(radioButtonBlue);
verticalLayout->addWidget(groupBox_2);
plainTextEdit = new QPlainTextEdit(Dialog);
plainTextEdit->setObjectName(QStringLiteral("plainTextEdit"));
verticalLayout->addWidget(plainTextEdit);
horizontalLayout = new QHBoxLayout();
horizontalLayout->setSpacing(6);
horizontalLayout->setObjectName(QStringLiteral("horizontalLayout"));
pushButtonClose = new QPushButton(Dialog);
pushButtonClose->setObjectName(QStringLiteral("pushButtonClose"));
horizontalLayout->addWidget(pushButtonClose);
horizontalSpacer = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);
horizontalLayout->addItem(horizontalSpacer);
pushButtonCancel = new QPushButton(Dialog);
pushButtonCancel->setObjectName(QStringLiteral("pushButtonCancel"));
horizontalLayout->addWidget(pushButtonCancel);
horizontalSpacer_2 = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);
horizontalLayout->addItem(horizontalSpacer_2);
pushButtonOK = new QPushButton(Dialog);
pushButtonOK->setObjectName(QStringLiteral("pushButtonOK"));
horizontalLayout->addWidget(pushButtonOK);
verticalLayout->addLayout(horizontalLayout);
retranslateUi(Dialog);
QObject::connect(pushButtonOK, SIGNAL(clicked()), Dialog, SLOT(accept()));
QObject::connect(pushButtonCancel, SIGNAL(clicked()), Dialog, SLOT(reject()));
QObject::connect(pushButtonClose, SIGNAL(clicked()), Dialog, SLOT(close()));
QMetaObject::connectSlotsByName(Dialog);
} // setupUi
void retranslateUi(QDialog *Dialog)
{
Dialog->setWindowTitle(QApplication::translate("Dialog", "DialogByDesigner", Q_NULLPTR));
groupBox->setTitle(QApplication::translate("Dialog", "GroupBox", Q_NULLPTR));
checkBoxUder->setText(QApplication::translate("Dialog", "Uderline", Q_NULLPTR));
checkBoxItalic->setText(QApplication::translate("Dialog", "Italic", Q_NULLPTR));
checkBoxBold->setText(QApplication::translate("Dialog", "Bold", Q_NULLPTR));
groupBox_2->setTitle(QApplication::translate("Dialog", "GroupBox", Q_NULLPTR));
radioButtonBlack->setText(QApplication::translate("Dialog", "Black", Q_NULLPTR));
radioButtonRed->setText(QApplication::translate("Dialog", "Red", Q_NULLPTR));
radioButtonBlue->setText(QApplication::translate("Dialog", "Blue", Q_NULLPTR));
pushButtonClose->setText(QApplication::translate("Dialog", "\351\200\200\345\207\272", Q_NULLPTR));
pushButtonCancel->setText(QApplication::translate("Dialog", "\345\217\226\346\266\210", Q_NULLPTR));
pushButtonOK->setText(QApplication::translate("Dialog", "\347\241\256\345\256\232", Q_NULLPTR));
} // retranslateUi
};
namespace Ui {
class Dialog: public Ui_Dialog {};
} // namespace Ui
QT_END_NAMESPACE
#endif // UI_DIALOG_H
总结
提示:这里对文章进行总结:
例如:以上就是今天要讲的内容,本文仅仅简单介绍了pandas的使用,而pandas提供了大量能使我们快速便捷地处理数据的函数和方法。