qt学习 实训第三天,第四天
来源:互联网 发布:淘宝直通车没流量 编辑:程序博客网 时间:2024/06/03 18:54
第三天:
1、简单的QMessageBox界面设计
按照之前的步骤建立一个新的工程Camera工程,之后在左边栏界面文件dialog.ui中双击,把Push Button拖拉到中间去,修改它的objectname,如BT_MSG,然后在Dialog.cpp中添加一个头文件QMessagebox,如下面:include ,然后在新出现的void Dialog::on_BT_MSG_clicked函数中填写QMessageBox::warning( this,”hehe”,”haha”);然后出现一个窗口中间有一个框,名字叫Push Button,点击这个按钮,就会出现一个窗口,窗口名字叫hehe,内容是haha,左边还有出现一个感叹号的标志。
然后可以试试QMessageBox的另外三个方法question,critical,information.
2.点击一个按钮,然后出现Save & Open两个按钮,分别出现出现两个不同的内容
在上一个代码中把BT_MSG的代码改一下,用if语句
if( QMessageBox::information(this,"Tips","Are you ok?",QMessageBox::Save,QMessageBox::Open) == QMessageBox::Save) QMessageBox::information(this,"heh","eah"); else QMessageBox::warning(this,"tian","bushi");
ps:注意括号。
3.试试,把另一个按钮设置成把一个图片变成黑白色的。
把上方代码的else的内容改成第二天学习的相对应的内容。
4.点击一个按钮,在窗口上出现一个新的按钮
用QPushButton定义一个新的对象BT_TEST和一个新的函数,然后在cpp文件中去定义,因为不知道这个按钮有多大,先在构造函数中定义这个对象BT_TEST为空,在析构函数中去删除,因为占内存,不删除,会出错。如果new了一个按钮,就必须在析构函数中去删除这个按钮。
private slots: void Picture();private: Ui::Dialog *ui; QPushButton* BT_TEST;----------Dialog::Dialog(QWidget *parent) : QDialog(parent), ui(new Ui::Dialog){ ui->setupUi(this); BT_TEST = NULL; connect(ui->BT_MSG,SIGNAL(clicked( )),this,SLOT(Picture()));}Dialog::~Dialog(){ delete ui; if(BT_TEST) delete BT_TEST;}void Dialog::Picture(){ if( ! BT_TEST ) { BT_TEST = new QPushButton( this ); BT_TEST->setText( "hee" ); BT_TEST->move(10,10); BT_TEST->show(); } ui->BT_MSG->setText("sdfsadf");//这个基本没用,改变原来按钮的文本}
connect(ui->BT_MSG,SIGNAL(clicked( )),this,SLOT(Picture()));
connect函数我认为第一个可认为为发射源,按钮BT_MSG点击它可以发射出一个信息,通过clicked信号再到picture函数中,用来连接两个按钮。具体的百度。
void Dialog::Picture()
新建立的函数这样定义。
5.让新出现的按钮也会出现提醒
在原函数下用connect与新的函数连接。connect(BT_TEST,SIGNAL(clicked() ),this,SLOT(testhehe()));
6.用label和lineedit以以上的代码同样建立方法,输入用户名和密码,再显示出来
void Dialog::on_BT_MSG_clicked(){ connect(ui->BT_MSG,SIGNAL(clicked()),this,SLOT(testhehe())); QMessageBox::information(this,"User Name",ui->LE_USERNAME->text() ); QMessageBox::information(this,"User passwd",ui->LE_PWD->text() );}void Dialog::testhehe(){ QMessageBox::information(this,"sfa","sdfa");}
7.`Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
ui->setupUi(this);
ui->LE_USERNAME->setText(“请输入用户名”);
}
Dialog::~Dialog()
{
delete ui;
}
void Dialog::on_BT_MSG_clicked()
{
connect(ui->BT_MSG,SIGNAL(clicked()),this,SLOT(testhehe()));
QMessageBox::information(this,”User Name”,ui->LE_USERNAME->text() );
QMessageBox::information(this,”User passwd”,ui->LE_PWD->text() );
}
void Dialog::testhehe()
{
QMessageBox::information(this,”sfa”,”sdfa”);
}`
8.建立一个QLineEdit的文本框
在头文件需要添加新的头文件#include <QLineEdit>
,这样才能正确的建立对象指针QLineEdit* LE_YEAR;
然后在构造函数中建立LE_YEAR的属性// 创建一个文本框对象
LE_YEAR = new QLineEdit( this );
// 调整文本框位置
LE_YEAR->move( 20, 0 );
// 设置文本框上显示的文本
LE_YEAR->setText( "2016" );
// 设置文本框的显示模式
LE_YEAR->setEchoMode( QLineEdit::NoEcho );
// 获取文本框的文本
QMessageBox::question( this, "info", LE_YEAR->text() );
之后在析构函数中删除这个对象delete LE_YEAR;
9.在板子上不能支持中文,所以在QT上需要添加如下代码:
首先在cpp文件中添加头文件#include <QTextCodec>
,在构造函数中添加`QTextCodec* code = QTextCodec::codecForName( “UTF-8” );
QTextCodec::setCodecForTr( code );QTextCodec::setCodecForLocale( QTextCodec::codecForLocale() );QTextCodec::setCodecForCStrings( QTextCodec::codecForLocale() );connect( ui->BT_SHOWIMAGE, SIGNAL( clicked() ), this, SLOT( ShowImage() ) );`
第四天:
1.简单的显示图片(没有设置大小和压缩,只是显示一张照片)
先拉取一个label定义为LB_IMAGE,在头文件dialog.h中定义一个对象指针QImage m_BKImage,然后在cpp文件中添加图片路径m_BKImage.load(**);和ui->LB_IMAGE->setPixmap(QPixmap::fromImage(m_BKImage));
m_BKImage.load( "E:\\google download\\haha.jpg");ui->LB_IMAGE->setPixmap( QPixmap::fromImage( m_BKImage));
然后可以简单的实现显示图片
2.通过一个按钮来实现显示图片
在头文件中添加一个函数ShowImage()和按钮PushButton的objectname:BT_SHOWIMAGE和显示图片的按钮label:LB_IMAGE;
在构造函数中使用connect连接按钮BT_SHOWIMAGE和函数ShowImage和函数ShowImage的实现。
Dialog::Dialog(QWidget *parent) : QDialog(parent), ui(new Ui::Dialog){ ui->setupUi(this); connect(ui->BT_SHOWIMAGE,SIGNAL(clicked() ),this,SLOT( ShowImage() )); //因为在connect中ShowImage被设置为SLOT槽函数,所以在头文件中就必须定义为private slot: (public slot也可以),必须为slot,其他的不行,比如public,无法识别为slot}void Dialog::ShowImage(){ if( ! m_BKImage.load( "E:\\google download\\haha.jpg")) { QMessageBox::question( this," Error","Load Picture Failed"); return; //不用返回0 } ui->LB_IMAGE->resize( m_BKImage.width(),m_BKImage.height());//设置宽高 ui->LB_IMAGE->setPixmap( QPixmap::fromImage( m_BKImage)); //加载图片 //然后改变一下标点,MOVE一下 ui->LB_IMAGE->move( 0,0 ); //到这里能显示图片了,但因为图片上还有lineedit显示,所以使用hide函数去取消; ui->BT_SHOWIMAGE->hide(); ui->lineEdit->hide(); }
为了自己自动设置路径,不能一直确定路径,所以在ShowImage函数中更改一下变量m_BKImage的路径:
if( ! m_BKImage.load( ui->lineEdit->text() )) //在lineedit中把Windows的路径放到其中,可以放粗来。 { QMessageBox::information(this,"sdf","gfdg"); } ui->LB_IMAGE->resize( m_BKImage.width(),m_BKImage.height());//设置宽高 ui->LB_IMAGE->setPixmap( QPixmap::fromImage( m_BKImage)); //加载图片 //然后改变一下标点,MOVE一下 ui->LB_IMAGE->move( 0,0 ); ui->BT_SHOWIMAGE->hide(); ui->lineEdit->hide();
3.自己添加路径,点开按钮可以弹出自动搜索路径,第二个只是自己知道路径,这个是搜索路径,然后放到lineedit中(并压缩图片)
用QString,首先添加头文件QFileDialog,定义一个变量fileName
压缩图片是在设置显示图片的时候resize和setPixmap中更改一下代码
void Dialog::ShowImage(){ QString fileName = QFileDialog ::getOpenFileName(this,tr("Open File"), "", tr("Images (*.png *.bmp *.jpg)")); //这个方法是打开相应的路径,变量名为fileName if( ! m_BKImage.load( fileName )) //在lineedit中把Windows的路径放到其中,可以放粗来。 { QMessageBox::information(this,"sdf","gfdg"); return; } ui->LB_IMAGE->resize( this->geometry().width(),this->geometry().height());//设置宽高 ui->LB_IMAGE->setPixmap( QPixmap::fromImage(m_BKImage.scaled(this->geometry().width(),this->geometry().height(),Qt::KeepAspectRatio))); //加载图片 //然后改变一下标点,MOVE一下 ui->LB_IMAGE->move( 0,0 ); ui->BT_SHOWIMAGE->hide(); ui->lineEdit->hide();}
4.让图片随客户框的大小的改变而改变
dialog.h:#ifndef DIALOG_H#define DIALOG_H#include <QDialog>namespace Ui {class Dialog;}class Dialog : public QDialog{ Q_OBJECTpublic: explicit Dialog(QWidget *parent = 0); ~Dialog(); void resizeEvent(QResizeEvent* size);private: Ui::Dialog *ui; QImage m_BKImage;private slots: void ShowImage();};#endif // DIALOG_H
dialog.cpp:#include "dialog.h"#include "ui_dialog.h"#include <QMessageBox>#include <QFileDialog>Dialog::Dialog(QWidget *parent) : QDialog(parent), ui(new Ui::Dialog){ ui->setupUi(this); connect(ui->BT_SHOWIMAGE,SIGNAL(clicked() ),this,SLOT( ShowImage() ));}Dialog::~Dialog(){ delete ui;}void Dialog::ShowImage(){QString fileName = QFileDialog ::getOpenFileName(this,tr("Open File"), "", tr("Images (*.png *.bmp *.jpg)")); if( ! m_BKImage.load( fileName )) //在lineedit中把Windows的路径放到其中,可以放粗来。 { QMessageBox::information(this,"sdf","gfdg"); return; } ui->LB_IMAGE->resize( this->geometry().width(),this->geometry().height());//设置宽高 ui->LB_IMAGE->setPixmap( QPixmap::fromImage(m_BKImage.scaled(this->geometry().width(),this->geometry().height(),Qt::KeepAspectRatio))); //加载图片 ui->LB_IMAGE->move( 0,0 ); ui->BT_SHOWIMAGE->hide(); ui->lineEdit->hide();}void Dialog::resizeEvent(QResizeEvent* size){ QString position; position = QString("%1 %2").arg(this->geometry().width()).arg(this->geometry().height()); ui->lineEdit->setText(position); ui->LB_IMAGE->resize( this->geometry().width(),this->geometry().height());//设置宽高 ui->LB_IMAGE->setPixmap( QPixmap::fromImage(m_BKImage.scaled(this->geometry().width(),this->geometry().height(),Qt::KeepAspectRatio))); //加载图片}
QStringQString(“%1 %2”).arg(this->geometry().width()).arg(this->geometry().height());其中的%1%2是指代在arg后的指针,%1是第一个,%2是第二个。
- qt学习 实训第三天,第四天
- Android学习(第三天、第四天)
- Qt学习第四天
- Qt学习第三天
- Qt学习第三天
- Qt 学习第四篇
- java基础第三天、第四天
- 嵌入式开发之C语言基础第三天&第四天学习笔记
- [记录]学习<<QT学习之路2>>第四天
- Qt学习日志-第四章
- 学习python的第四十三天-第六章 字符串
- C程序设计 第四版(谭浩强)-学习笔记-第三天
- 编程第四十三天
- [记录]<<QT学习之路2>>学习第三天
- 【每日工作-第三天、第四天】Java语法
- qt学习第四课:子类化窗口
- qt第三天
- 实习日记->第四十三天
- Android studio 中获取SHA1的方法
- 用模板实现单链表
- canvas分享之用户交互
- static修饰属性
- ContentProvider
- qt学习 实训第三天,第四天
- 如何优化appium-IOS的自动化测试运行的速度
- 如何用maven创建nexus私服并将包打包发布到私服
- java中String 的==和equals;String直接赋值和使用new String()
- Java常考面试题
- 坚持#第24天~回黄陂了
- Intent学习(1)
- How To Use Codelite
- READING NOTE: Rethinking the Inception Architecture for Computer Vision