Qt学习笔记

1.信号与槽关联是用QObject::connect()函数实现的,其基本格式是:

QObject::connect(sender, SIGNAL(signal()), receiver, SLOT(slot()));
sender是发射信号的对象的名称,receiver是接受信号的对象名称,signal()是信号名称,slot是槽函数
connect(pushButton,SIGNAL(clicked()),this,SLOT(close()));

一个信号可以连接另一个信号(说明connect万物皆可连)

connect(pushButton, SIGNAL(objectNameChanged(QString)),this, SIGNAL(windowTitelChan
ged(QString)));

在使用信号与槽的类中,必须在类的定义中加入宏 Q_OBJECT(特别重要)。
只有当信号关联的所有槽函数执行完毕后,才会执行发射信号处后面的代码。

断开连接的的格式:

bool QObject::disconnect(const QObject *sender, const char *signal, const QObject *receiver, const 
char *method)

与指定的接收者断开连接:

disconnect(myObject, 0, myReceiver, 0);

信号只需声明无需定义,声明槽函数必须写槽的定义(定义指函数体的实现):
槽可以是任何成员函数、普通全局函数、静态函数
槽函数和信号的参数和返回值要一致

#include <QPushButton>
signals:
	/*声明一个信号,无需定义*/
	void pushButtonTextChanged();
public slots:
	/*声明一个槽函数*/
	void changeButtonText();
	/*声明按钮点击的槽函数*/
	void pushButtonClicked();
private:
	/*声明一个对象pushButton*/
	QPushButton *pushButton;

2.普通按钮QPushButton用法

#include <QPushButton>
this->setGeometry(0, 0, 800, 480);  //宽高800*400,原点在(0,0)

pushButton = new QPushButton("窗口皮肤", this);  //实例化按钮对象

/*槽函数实现*/
void MainWindow::pushButton_Clicked()
{
	/*设置主窗口的样式*/
	this->setStyleSheet("QMainWindow{background-color:rgba(238, 122, 233, 100%);}");
}

3.工具按钮QToolButton

#include <QToolButton>
#include <QToolBar>
#include <QStyle>

this->setGeometry(0, 0, 800, 480);

/*实例化QToolBar对象*/
toolBar = new QToolBar(this);
/*设置toolBar的位置和大小*/
toolBar->setGeometry(0, 0, 800, 100);

/*实例化QStyle类对象,用于设置风格,调用设置风格,调用系统类自带的图标*/
QStyle *style = QApplication::style();

/*使用Qt自带的标准图标,可以在帮助文档里搜索QStyle::StandardPixmap*、
QIcon icon = style->standardIcon(QStyle::SP_TitleBarContextHelpButton);

toolButton = new QToolButton();

/*设置图标*/
toolButton->setIcon(icon);

/*设置要显示的文本*/
toolButton->setText("帮助");

/* 调用setToolButtonStyle()方法,设置toolButton的样式,设置为文本置于图标下方*/
toolButton->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);

/*最后将toolButton添加到ToolBar里*/
toolBar->addWidget(toolButton);

4.QRadioButton控件(提供一个带有文本标签的单选框(单选按钮))
QRadioButton是一个可以切换选中(checked)或选中(uncheeked)转态的选项按钮。单选框呈现给用户"多选一"的选择。

#include <QRadioButton>

5.QCheckBox(复选按钮提供多选多)

6.QCommandLinkButton(命令连接按钮,除带有正常的按钮上的文字描述文本外,默认情况下,它将携带一个箭头图标,表明按下按钮将打开另一个窗口或页面)

#include <QCommandLinkButton>
#include <QUrl>

/*实例化对象*/
commandLinkButton = new QCommandLinkButton("打开/home目录", "点击此处将调用系统的窗口/home目录", this);
/*设置QCommandLinkButton位置和显示大小*/
commandLinkButton->setGeometry(300, 200, 250, 60);
/*信号与槽连接*/
connect(commandLinkButton, SIGNAL(clicked()), this, SLOT(comandLinkButtonClicked()));

void MainWindow::comandLinkButtonClicked()
{
	//调用系统服务打开/home目录
	QDesktopService::openUrl(Qurl("file::home/"));
}

7.QDialogButtonBox(对话框和消息框)
QDialogButtonBox允许开发向其添加按钮,并将自动使用适合用户桌面环境的布局。

button_Box = new QDialogButtonBox(QDialogButtonBox::Ok
 | QDialogButtonBox::Cancel
 | QDialogButtonBox::Open
 | QDialogButtonBox::Save
 | QDialogButtonBox::Close
 | QDialogButtonBox::Discard
 | QDialogButtonBox::Apply
 | QDialogButtonBox::Reset
 | QDialogButtonBox::RestoreDefaults
 | QDialogButtonBox::Help
 | QDialogButtonBox::SaveAll);
#include <QDialogButtonBox>
#include <QPushButton>

dialogButtonBox = new QialogButtonBox(this);
dialogButton->setGeometry(300, 200, 200, 30);
private:
	QDialogButtonBox *dialogButtonBox;
	QPushButton *pushButton;
private slots:
	/*声明信号槽,带QAbstractButton*参数,用于判断点击了哪个按钮*/
	void dialogButtonBoxClicked(QAbstractButton *);

//主窗口设置位置和显示的大小
this->setGeometry(0,0,800,480);

//实例化并设置按钮的盒子的大小和位置
dialogButtonBox = new QDialogButtonBox(this);
dialogButtonBox ->setGeometry(300,200,200,30);

//使用Qt的Cancel按钮
dialogButtonBox->addButton(QDialogButtonBox::Cancel);  
//原型 void QDialogButtonBox::addButton(QAbstractButton *button, QDialogButtonBox::ButtonRole role)。

//将英文Cancel按钮设置为中文取消
dialogButtonBox->button(QDialogButtonBox::Cancel)->setText("取消");

//设定位置与大小
pushButton = new QPushButton(tr("自定义"));

//将pushButton添加到dialogButtonBox,并设定ButtonRole为ActionRole
dialogButtonBox->addButton(pushButton, QDialogButtonBox::ActionRole);

//信号槽链接,带参数QAbstractButton *, 用于判断用户点击哪个按键
connect(dialogButtonBox, SIGNAL(clicked(QAbstractBUtton *)), this, SLOT(dialogButtonBoxClicked(QAbstractButton *)));

void MainWindows::dialogButtonBoxClicked(QAbstractButton *button)
{
	if(button == dialogButtonBox->button(QDialogButtonBox::Cancel))
	{
		qDebug() << "单击取消键" <<endl;
	}else if(button == pushButton)
	{
		qDebug() << "单击自定义建" <<endl;
	}
}

7.QLabel
QLabel提供了一种用于文本或图像显示的小部件,可用显示文本和图像

#include <QLabel>

private:
	/*QLabel对象显示字符串*/
	QLabel *labelString;
	/*QLabel对象用于显示图像*/
	QLabel *labelImage;

//使用资源里的文件时格式是:+前缀+文件路径
QPixmap pixmap(":images/openedv.png");

labelImage = new QLabel(this);

//标签大小为452*132,根据图像的大小来设置,为了避免图像被压缩或者拉伸
labelImage->setGeometry(180, 150, 452, 132);
//设置图像
labelImage->setPixmap(pixmap);
//开启允许缩放填充
labelImage->setScaledContents(true);

labelString = new Qlabel(this);
labelString->setText("标签演示文本");
labelString->setGeometry(300, 300, 100, 20);

8.QCalendarWidget
QCalendarWidget继承QWidget,提供了一个基于月的日历小部件,允许用户选择日期,还提供了几个公共插槽来更改显示的年份和月份。

#incldue <QCalendarWidget>
#include <QPushButton>
#include <QLabel>

private:
	QCalendarWidget *calendarWidget;
	QPushButton *pushButton;
	QLabel *lable;
private slots:
	void calendarWidgetSelectionChanged();
	void pushButtonClicked();

//对象实例化并设置显示的位置与大小
calendarWidget = new QCalendarWidget(this);
calendarWidget->setGeometry(200, 20, 400, 300);

QFont font;
//设置日历里字体的大小为10像素
font.setPixelSize(10);
calendarWidget->setGont(font);

pushButton = new QPushButton("回到当前日期",this);
pushButton->setGeometry(200, 350, 100, 30);

label = new QLabel(this);
label->setGeometry(400, 350, 400, 30);
QString str = "当前选择的日期:" + calendarWidget->selectedDate().toString();
label->setText(str);


void MainWindow::calendarWidgetSelectionChanged()
{

	//当日历点击改变当前选择的日期,更新Label的显示内容
	QString str = "当前选择的日期" + calendarWidget->selectedDate().toString();
	label->setText(str);
}

void MainWindows::pushButtonClicked()
{
	//设置当前选定的日期为系统的QDate
	calendarWidget->setSelectedDate(QDate::currentDate());
}

9.QLCDNumber
QLCDBumber继承QFrame,显示一个类似于lcd的数子,可以显示任意大小的数字,可以显示十进制,十六进制,八进制,二进制。使用display()插槽很容易连接到数据源,该插槽被重载以接受五种参数类型中的任何一种。

如下使用LCDNumber控件作时钟的显示,与一个定时器更新LCDNumber时间

#include <QLCDNumber>
#include <QTimer>
#include <QTime>

private:
	QLCDNumber *lcdNumber;
	QTimer *timer;
private slots:
	void timerTimeOut();

lcdNumber = new QLCDNumber(this);
lcdNumber->setGemotry(300, 200, 200, 50);

//设置显示的位数8位
lcdNumber->setDigitCount(8);
//设置样式
lcdNumber->setSegmentStyle(QLCDNumber::Flat);

//设置lcd显示为当前系统时间
QTime time = Qtime::currentTime();

//设置显示的样式
lcdNumber->display(time.toString("hh:mm:ss"));

timer = new QTimer(this);
//设置定时器1000ms发送一个timeout()信号
time->start(1000);

connect(timer, SIGNAL(timeout()), this, SLOT(timerTimeOut()));

void MainWindow::timerTimeOut()
{
	//定定时器计时1000ms后,刷新lcd当前显示系统时间
	QTime time = QTime::currentTime();
	//设置显示的样式
	lcdNumber->display(time.toString("hh:mm:ss"));
}

10.QProgressBar
QProgressBar继承QWidget,提供了一个水平或垂直的进度条。进度条用于显示用户显示操作的进度,并向他们确认应用程序仍在运行。

#include <QProgressBar>
#include <QTimer>

private:
	QProgressBar *progressBar;
	QTimer *timer;
//用于设置当前QProgressBar的值
int value;

private slots:
	void timerTimeOut();

progressBar = new QProgressBar(this);
progressBar->setGeometry(300, 200, 200, 60);

//样式表设置,常用使用setStyleSheet来设置样式(实现界面美化的功能)
progressBar->setStyleSheet(
										"QProgressBar{border:8px solod #FFFFFF:}"
										"height:30"
										"border-image:url(:/images/battery.png);" //背景图片
										"text-align:center;" // 文字居中
										"color:rgb(255,0,255);"
										);

//设置progressBar的范围值
progressBar->setRange(0, 100);

value = 0;
//给progressBar设置当前值
progressBar->setValue(value);
//设置当前文本字符串的显示格式
progressBar->setFormat("充电中%p");

//定时器实例化设置每100ms发送一个timeout信号
timer = new QTimer(this);
timer->start(100);

connect(timer, SIGNAL(timeout), this, SLOT(timerTimeOut()));

void MainWindow::timerTimeOUt()
{
	value++;
	progreassBar->setValue(value);
	if(value>100)
		value = 0;
}

11.QFrame
QFrame继承QWidget,QFrame类是有框架的窗口部件的基类,它绘制框架并且调用一个虚函数drawContents()来填充这个框架。

private:
	QFrame *hline;
	QFrame *vline;

hline = new QFrame(this);
//确定起始点,设置长和宽,绘制矩形
hline->setGeometry(QRect(200, 100, 400, 40));
//设置框架样式为HLine,水平
hline->setFrameShape(QFrame::HLine);
//绘制阴影
hline->setFrameShadow(QFrame::Sunken);

vline = new QFrame(this);
//确定起始点,设置长和宽,绘制矩形
vline ->setGeometry(QRect(200, 100, 400, 40));
//设置框架样式为HLine,水平
vline ->setFrameShape(QFrame::VLine);
//绘制阴影
vline ->setFrameShadow(QFrame::Sunken);

12.QTextBrowser
QTextBrowser继承QTextEdit,提供了一个具有超文本导航的文本浏览器。

#include <QTextBrowser>
//窗口对话框与文本流
#include <QFileDialog>
#include <QTextStream>

private:
	UI::MainWindow *ui;
	
	QTextBrowser *textBrowser;
	QAction *openAction;

private slots:
	void openActionTriggered();


ui->setupUI(this);
//设置主窗口位置与大小
this->setGemotry(0, 0, 800, 480);
//将窗口标题设置为文本浏览器
tihs->setWindowTitle("文本浏览器");

textBrowser = new QTextBrowser(this);
//将文本浏览器窗口居中
this->setCentralWidget(textBrowser);

openAction = new QAction("打开"this);
/* ui 窗口自带有 menubar(菜单栏)、mainToolbar(工具栏)与
* statusbar(状态栏)
 * menuBar 是 ui 生成工程就有的,所以可以在 menubar 里添加
* 我们的 QActiont 等,如果不需要 menubar,可以在 ui 设计
 * 窗口里,在右则对象里把 menubar 删除,再自己重新定义自己的
* 菜单栏
*/
/* 将动作添加到菜单栏 */
//将动作添加到菜单栏
ui->menubar->addAction(openAction);

connect(openAction, SIGNAL(triggered()), this, SLOT(openActionTriggered()));

void MainWindow::openActionTriggered()
{
	//调用系统打开文件窗口,过滤文件名
	QString fileName = QFileDialog::getOpenFileName(this, tr("打开文件"), "", tr("Files(*.txt *.cpp *.h *.html *.htm)"));
	QFile myFile(fileName);
	//以只读、文本方式打开,若打开失败,则返回
	if(!myFile.open(QIODevice::ReadOnly | QIODevice::Text))
		return;
	
	//用QTextStream对象接受
	QTextStream in(&myFile);
	//读取全部数据
	QString myText = in.readAll();
	/* 判断打开文件的后缀,如果是 html 格式的则设置文本浏览器为 html 格式 */
	if(fileName.endsWith("html") || fileName.endsWith("htm"))
	{
		textBrowser->setHtml(myText);
	}else
	{
		textBrowser->setPlainText(myText);
	}
	/* ui 窗口自带有 statusbar(状态栏),设置打开的文件名 */
	ui->status->showMessage("文件名:" + fileName);

}

13.QGrapHicsVie
QGraphicsView继承QAbstractScrollArea,QGraphicsVies是图形视图框架的一部分,它提供了基于图元的模型/视图编程。

14.QFormLayout
QFormLayout继承QLayout,QFormLayout类管理输入小部件及其关联标签的表单。QFormLayout是一个方便的布局类,它以两列的形式布局其子类。左列由标签组成,右列由字段小部件(QLineEdit(行编辑器)、QSpinBox(旋转框等))组成。通常使用 setRowWrapPolicy(RowWr
apPolicy policy)接口函数设置布局的换行策略进行布局等。

#include <QFromLayout>
#include <QLineEdit>

private:
	//widget对象
	QWidget *fWidget;
	//用于输入用户名
	QLineEdit *userLineEdit;
	//用于输出密码
	QLineEdit *passwordLineEdit;
	
	QFromLayout *formLayout;

fWidget = new QWidget(this);
fWidget->setGeometry(250, 100, 300, 200);

userLineEdit = new QLineEdit();
passwordLineEdit = new QLineEdit();

formLayout = new QFormLayout();

//添加行
formLayout->addRow("用户名:", userLineEdit);
formLayout->addRow("密码:", passwordLineEdit);

//设置水平垂直间距
formLayout->setSpacing(10);

//设置布局外框的宽度
formLayout->setMargin(20);

//将formLayout布局到fWidget
fWidget->setLayout(formLayout);


14.QSpacerItem
QSpacerItem继承QLayoutItem。QSpacerItem类在布局中提供空白(空白间隔)。所以在布局中使用

15.QFile读写文本
QFile是一个读写文本、二进制文件和资源I/O设备。QFile可以自己使用,也可以更方便地与QTextStream和QDataStream一起使用。
文件名通常在构造函数中传递,但他可以在任何时候使用setFileName()设置,文件的路径都是‘/’。
使用exists()检查文件是否存在,并使用remove()删除文件。(更高级的文件系统相关操作由QFileInfo和QDir提供。)用open()打开文件,close()关闭文件,flush()刷新文件。
通常使用QDataStream或QTextStream读写数据,但也可以调用QIODevice继承的函数read()、readLine()、readAll()、write().
使用pos()获取当前文件位置,可以使用seek()移动到新的文件位置。如果已经到达文件的末尾,则atEnd()返回true。

QFile::open()函数打开文件时需要传递QIODevice::OpenModeFlag枚举类型的参数,决定文件以什么方式打开:
QIODevice::ReadOnly:已只读方式打开文件,用于载入文件
QIODevice::QriteOnly:只写方式,用于保存文件
QIODevice::ReadWrite:已读写方式打开
QIODevice::Append:以添加模式打开,新写入文件的数据添加到文件尾部
QIODevice::Truncate:以截取方式打开文件,文件原有内容全部删除
QIODevice::Text:以文本方式打开文件,读取时"\n"被自动翻译为换行符,写入时字符串结束符会自动翻译为系统平台的编码,如windows平台下是"\r\n"
在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值