基于VS的QT常用控件的使用方法

基于VS的QT常用控件的使用方法
#pragma execution_character_set(“utf-8”)//使vs里的QT输出不乱码的中文
#include “MainWindow.h”
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
MainWindow::MainWindow(QWidget *parent): QMainWindow(parent)
{
//菜单栏
QMenuBar *mBar = menuBar();
setMenuBar(mBar);
//添加菜单
QMenu *pFile = mBar->addMenu(“文件”);

//添加菜单项,添加动作
QAction *p1 = pFile->addAction("模态对话框");
connect(p1, &QAction::triggered,
	[=]()
{
	QDialog  dlg;
	dlg.exec();
	qDebug() << "模态对话框";
});
QAction *p2 = pFile->addAction("非模态对话框");
connect(p2,&QAction::triggered,
	[=]()
{
	
	//dlg1.show();
	//qDebug() << "非模态对话框";
	//QDialog *dlg2 = new QDialog(this);
	//dlg2->show();
	QDialog *dlg3 =new QDialog;
	dlg3->setAttribute(Qt::WA_DeleteOnClose);
	dlg3->show();
});
QAction *pNew=pFile->addAction("新建");
connect(pNew,&QAction::triggered,
	[=]()
{
	
	qDebug() << "新建被按下";
});
//标准对话框
QAction *p3 = pFile->addAction("关于对话框");
connect(p3,&QAction::triggered,
	[=]()
{
	QMessageBox::about(this, "about", "关于QT");
});
QAction *p4 = pFile->addAction("问题对话框");
connect(p4,&QAction::triggered,
	[=]()
{
	int ret=QMessageBox::question(this, "question", "Are you ok?",
		QMessageBox::Ok|QMessageBox::Cancel|QMessageBox::Yes);
	switch (ret)
	{
	case QMessageBox::Yes:
		qDebug() << "i am ok";
		break;
	case QMessageBox::Ok:
		qDebug() << "i am bad";
		break;
	case QMessageBox::Cancel:
		qDebug() << "i am not bad";
		break;
	default:
		break;
	}
});
//文件对话框
QAction *p5 = pFile->addAction("文件对话框");
connect(p5, &QAction::triggered,
	[=]()
{
	QString path=QFileDialog::getOpenFileName(this, "open", "../",
		"souce(*.cpp *.h);;Text(*.txt);;all(*.*)");
	qDebug() << path;
});
pFile->addSeparator();//添加分割线
QAction *pOpen = pFile->addAction("打开");

//工具栏,菜单项的快捷方式
QToolBar *toolBar = addToolBar("toolBar");
//工具栏添加快捷键
toolBar->addAction(pNew);
QPushButton *b = new QPushButton(this);
b->setText("^_^");
//添加小控件
toolBar->addWidget(b);
connect(b,&QPushButton::clicked,
	[=]()
{
	b->setText("**");
});

//状态栏
QStatusBar *statuBar = statusBar();
QLabel *label = new QLabel(this);
label->setText("Normal text file");
statuBar->addWidget(label);
statuBar->addWidget(new QLabel("2", this));
statuBar->addPermanentWidget(new QLabel("3", this));

//核心控件
QTextEdit *textEdit = new QTextEdit(this);
setCentralWidget(textEdit);

//浮动窗口
QDockWidget *dock = new QDockWidget(this);
addDockWidget(Qt::RightDockWidgetArea,dock);
//给浮动窗口添加控件
QTextEdit *textEdit1 = new QTextEdit(this);
dock->setWidget(textEdit1);

//ui.setupUi(this);

}

你可能感兴趣的:(QT)