QT5的一些常用函数(菜单,工具条,label,QMessageBox,定时器)
2017-09-07 14:21
513 查看
最近用QT做UI确实感觉漂亮和方便,常会用到一些小技巧,总结一下,方便以后的快速使用。
1.添加菜单
#include <QMenu>
QMenu* menu_file = new QMenu("File");
menu_file->addAction("Start",this,SLOT(openCamera()));
menu_file->addAction("Stop");
menu_file->addAction("Quit");
QMenu* menu_edit = new QMenu("Edit");
menu_edit->addAction("Show feature points",this,SLOT(show_features()));
menu_edit->addAction("Save face image",this,SLOT(SaveFaceDetected()));
QMenu* menu_help = new QMenu("Help");
menu_help->addAction("Technical Support");
menu_help->addAction("Contact us");
ui->menuBar->addMenu(menu_file);
ui->menuBar->addMenu(menu_edit);
2.添加工具条
#include <QToolBar>
#include <QAction>
QAction* new_Action = new QAction(QIcon(":/icons/big_icons/new.png") ,tr("&Add New..."),this);
new_Action->setStatusTip(tr("Add a new tool !"));
connect(new_Action,&QAction::triggered,this,MainWindow::add_new_tool);
ui->mainToolBar->addAction(new_Action);
3.开定时器
#include <QTimer>
QTimer* show_timer = new QTimer();
connect(show_timer,SIGNAL(timeout()),this,SLOT(updateTimer()));
show_timer->start(1000);
//需要定义和实现槽函数 updateTimer()
private slots:
void updateTimer();
4.label设置常规属性(label显示图像)
#include <QLabel>
Mat person = Mat::zeros(200,150,CV_8UC3);
cv::cvtColor(person,person,CV_BGR2RGB);
img = QImage((const unsigned char*)(person.data),person.cols,person.rows,QImage::Format_RGB888)
ui->label->setFrameShape(QFrame::Box);//设置边框
ui->label->setStyleSheet("border: 2px solid #00ff00");//边框颜色
ui->label->setScaledContents(true);
ui->label->setPixmap(QPixmap::fromImage(img));
QString name = "姓名: ";
ui->label->setText(name);
//设置字号
QFont name_ft;
name_ft.setPointSize(10);
ui->label->setFont(name_ft);
//设置颜色
QPalette pa;
pa.setColor(QPalette::WindowText,Qt::blue);
ui->label->setPalette(pa);解决显示图像扭曲的问题:void MyDisplayWidget::show_image(Mat im)
{
if(im.empty())
return;
cv::cvtColor(im, im, CV_BGR2RGB);
qt_image = QImage((const unsigned char*)im.data,
im.cols,
im.rows,
im.cols*im.channels(),
QImage::Format_RGB888);
im_label->setPixmap(QPixmap::fromImage(qt_image));
}5.QMessageBox的使用
#include <QMessageBox>
QMessageBox::information(this,
tr("error"),
tr("Open camera 0 failed!"));
1.添加菜单
#include <QMenu>
QMenu* menu_file = new QMenu("File");
menu_file->addAction("Start",this,SLOT(openCamera()));
menu_file->addAction("Stop");
menu_file->addAction("Quit");
QMenu* menu_edit = new QMenu("Edit");
menu_edit->addAction("Show feature points",this,SLOT(show_features()));
menu_edit->addAction("Save face image",this,SLOT(SaveFaceDetected()));
QMenu* menu_help = new QMenu("Help");
menu_help->addAction("Technical Support");
menu_help->addAction("Contact us");
ui->menuBar->addMenu(menu_file);
ui->menuBar->addMenu(menu_edit);
ui->menuBar->addMenu(menu_help);
2.添加工具条
#include <QToolBar>
#include <QAction>
QAction* new_Action = new QAction(QIcon(":/icons/big_icons/new.png") ,tr("&Add New..."),this);
new_Action->setStatusTip(tr("Add a new tool !"));
connect(new_Action,&QAction::triggered,this,MainWindow::add_new_tool);
ui->mainToolBar->addAction(new_Action);
3.开定时器
#include <QTimer>
QTimer* show_timer = new QTimer();
connect(show_timer,SIGNAL(timeout()),this,SLOT(updateTimer()));
show_timer->start(1000);
//需要定义和实现槽函数 updateTimer()
private slots:
void updateTimer();
4.label设置常规属性(label显示图像)
#include <QLabel>
Mat person = Mat::zeros(200,150,CV_8UC3);
cv::cvtColor(person,person,CV_BGR2RGB);
img = QImage((const unsigned char*)(person.data),person.cols,person.rows,QImage::Format_RGB888)
ui->label->setFrameShape(QFrame::Box);//设置边框
ui->label->setStyleSheet("border: 2px solid #00ff00");//边框颜色
ui->label->setScaledContents(true);
ui->label->setPixmap(QPixmap::fromImage(img));
QString name = "姓名: ";
ui->label->setText(name);
//设置字号
QFont name_ft;
name_ft.setPointSize(10);
ui->label->setFont(name_ft);
//设置颜色
QPalette pa;
pa.setColor(QPalette::WindowText,Qt::blue);
ui->label->setPalette(pa);解决显示图像扭曲的问题:void MyDisplayWidget::show_image(Mat im)
{
if(im.empty())
return;
cv::cvtColor(im, im, CV_BGR2RGB);
qt_image = QImage((const unsigned char*)im.data,
im.cols,
im.rows,
im.cols*im.channels(),
QImage::Format_RGB888);
im_label->setPixmap(QPixmap::fromImage(qt_image));
}5.QMessageBox的使用
#include <QMessageBox>
QMessageBox::information(this,
tr("error"),
tr("Open camera 0 failed!"));
相关文章推荐
- Qt开发中字符串、字节数组和变量常用的一些函数总结
- 分享一些常用的定时器运动函数
- Qt开发中字符串、字节数组和变量常用的一些函数总结
- Qt中一些常用时间显示的函数调用示例
- Tensorflow一些常用基本概念与函数
- c++一些常用函数的积累
- javascript一些常用函数
- PHP中的一些常用函数
- cocos2dx(v2.x)与(v1.x)的一些常用函数区别
- Tensorflow一些常用基本概念与函数(1)
- Oracle常用的一些语法、命令和函数
- Matlab GUI设计中的一些常用函数
- 2015-01-15 【SQLite】一些常用函数
- 一些常用的NLTK频率分布类中定义的函数
- 面向对象的asp编程之四--一些常用的函数
- 我常用的一些ASP自定义函数
- QListWidget的一些常用函数
- Qt入门之常用Qt标准对话框之QMessageBox
- jQuery 2 一些常用的函数
- C/C++ 常用的一些函数