qt学习 实训第五天 第六天

第五天:
1.表示时间延迟

添加头文件QTimer,槽函数OnTimeOut,还有对象指针QTimer m_TimerFlush,

Dialog::Dialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dialog)
{
    ui->setupUi(this);
    m_TimeFlush.setInterval( 20 );  //计时
    m_TimeFlush.start();

    connect( &m_TimeFlush,SIGNAL(timeout()),this,SLOT(OnTimeOut())); //给程序一个消息,会一直弹出窗口,无法相应,只能强行的退出
}
void Dialog::OnTimeOut()
{
    QMessageBox::information(this,"info","toue out now");
 }

2.在label上显示数字,随时间的进行而变化

在头文件定义一个数字uint count,在构造函数中定义一个初始值,在OnTimeOut的函数中让count变化

Dialog::Dialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dialog)
{
    ui->setupUi(this);
    m_TimeFlush.setInterval( 20 );  //计时
    m_TimeFlush.start();

    connect( &m_TimeFlush,SIGNAL(timeout()),this,SLOT(OnTimeOut())); //给程序一个消息,会一直弹出窗口,无法相应,只能强行的退出
    count =0;
}
void Dialog::OnTimeOut()
{
    ui->LE_TIME->setText( QString("%1").arg(count++));
}

3.按照时间的改变而图片变化:奇数时间是一张图片,偶数时间是另一张图片。
添加头文件#includ
定义新对象指针QImage m_BKImage;
改变一下OnTimeOut函数:

if(count++ %2==0)
    {
            m_BKImage.load( "E:\\google download\\haha.jpg");
            ui->LE_TIME->resize( m_BKImage.width(),m_BKImage.height());//设置宽高
            ui->LE_TIME->setPixmap( QPixmap::fromImage( m_BKImage));   //加载图片
            ui->LE_TIME->move( 0,0 );
     }
    else
    {
            m_BKImage.load( "C:\\Users\\Public\\Pictures\\Sample Pictures\\daa.jpg");
            ui->LE_TIME->resize( m_BKImage.width(),m_BKImage.height());//设置宽高
            ui->LE_TIME->setPixmap( QPixmap::fromImage( m_BKImage));   //加载图片
            ui->LE_TIME->move( 0,0 );
     }

4.显示板子当前的日期时间

使用QDateTime头文件即可,在OnTimeOut函数中改变:

    ui->LE_TIME->setText( QString("%1").arg(count++));
    QDateTime time = QDateTime::currentDateTime();
    ui->LE_TIME->setText( time.toString("yyyy-MM-dd hh:mm:ss.zzz"));
    //ui->LE_TIME->setText( QString("%1").arg(time.date().year()));
    //ui->LE_TIME->setText( QString("%1").arg(time.date().month()));  //dayOfWeek    dayOfYear
    //ui->LE_TIME->setText( QString("%1 %2").arg(time.date().day()).arg(time.time().hour()));
    //ui->LE_TIME->setText( QString("%1").arg(time.time().hour()));
    //ui->LE_TIME->setText( QString("%1").arg(time.toTime_t()));

改变时间的变化,不能没有变化,所以ui->LE_TIME->setText( QString("%1").arg(count++)); 下面注释掉的可以一个一个试试。而且点击label,在右边搜索区搜索font改变一下label的属性,文字格式的改变

5.使用LCD Number显示时间

设置LCD Number为LCD_DATE,在构造函数中添加

ui->LCD_DATE->setDigitCount(12);
    ui->LCD_DATE->setSegmentStyle(QLCDNumber::Flat);
    m_TimeFlush.setInterval(10);
    m_TimeFlush.start();

    connect(&m_TimeFlush,SIGNAL(timeout()),this,SLOT(OnTimeOut()));
    OnTimeOut();

然后在OnTimeOut的函数中为

 QDateTime time = QDateTime::currentDateTime();
 ui->LCD_DATE->display( time.toString("hh:mm:ss.zzz"));

第六天:

1.学习使用RadioButton

建立一个pushbutton,和radiobutton,并建立组
代码任务为:
1.建立一个组,让Fruit下的选项和select 下的选项能分开选择,并分别能显示出不同的提示。
2.在select选项中选择哪一项则按钮中显示哪项的文本。

ps:1.#include 可以建立 一个组,与别的radiobutton区别起来,可以分开的显示出来,单独的点击,比如在下面代码中,Fruit和man&woman分开,分别点击各自的选项,但是得单独的要把要区别出来的Fruit选项在构造函数中分别一个一个的添加并分配ID号。

dialog.h:
#ifndef DIALOG_H
#define DIALOG_H

#include 
#include 
namespace Ui {
class Dialog;
}

class Dialog : public QDialog
{
    Q_OBJECT

public:
    explicit Dialog(QWidget *parent = 0);
    ~Dialog();

private slots:
    void OnBtMsgClicked();

    void OnBtFruitClicked();


    void OnRbFruitCliced();

private:
    Ui::Dialog *ui;

    QButtonGroup* m_BqFruit;

    uint m_CheckedId;
};

#endif // DIALOG_H
dialog.cpp:
#include "dialog.h"
#include "ui_dialog.h"
#include 

Dialog::Dialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dialog)
{
    ui->setupUi(this);

    m_CheckedId=-1;

    connect(ui->BT_MSG,SIGNAL(clicked() ),this,SLOT(OnBtMsgClicked()));

    ui->RB_MAN->setChecked(true);

    connect(ui->BT_Fruit,SIGNAL(clicked() ),this,SLOT(OnBtFruitClicked()));

    m_BqFruit=NULL;
    m_BqFruit=new QButtonGroup (this);
    if(! m_BqFruit)
        exit(1);

    m_BqFruit->addButton( ui->RB_Apple,0);
    m_BqFruit->addButton(ui->RB_Pear,1);
    m_BqFruit->addButton(ui->RB_Cherry,2);

    connect(ui->RB_Apple,SIGNAL(clicked() ),this,SLOT(OnRbFruitCliced()));
    connect(ui->RB_Pear,SIGNAL(clicked() ),this,SLOT(OnRbFruitCliced()));
    connect(ui->RB_Cherry,SIGNAL(clicked() ),this,SLOT(OnRbFruitCliced()));
}

Dialog::~Dialog()
{
    delete ui;
    if( m_BqFruit)
        delete m_BqFruit;
}
void Dialog::OnBtMsgClicked()
{
    if(ui->RB_MAN->isChecked() )
       // QMessageBox::information(this,"info","You selectes man");
        ui->BT_MSG->setText("MAN");
    else if(ui->RB_WOMAN->isChecked())
       // QMessageBox::information(this,"info","you selext woman");
        ui->BT_MSG->setText("WOMAN");
    else
        QMessageBox::warning(this,"Warning","You are no selected");
}
void Dialog::OnBtFruitClicked()
{
   QString info = "You selected ";

   /*if(ui->RB_Apple->isChecked())
       info +="apple";
   else if(ui->RB_Cherry->isChecked())
       info +="cherry";
   else if(ui->RB_Pear->isChecked())
       info += "pear";*/

   switch(m_CheckedId)
   {
   case 0:info+="Apple";break;
   case 1:info+="Pear";break;
   case 2:info+="Cherry";break;
   }

     QMessageBox::warning(this," info",info);
}
void Dialog::OnRbFruitCliced()
{
    m_CheckedId = m_BqFruit->checkedId();
}

2.控件的综合应用

任务:建立两个lineedit和两个PushButton,和两个RadioButton,让在lineedit中输入路径,并选择radiobutton的选项选择哪一个按下一个按钮可以显示出来图片,按下另一个按钮可以选择路径,并在lineedit中可以显示路径信息。

PS:把两个选项radiobutton分别的区别成一个小组,再分别出来显示不同的图片,把radiobutton与分配ID号的函数连接起来,再用ID号的不同把各自的不同图片显示出来。

dialog.h:
#ifndef DIALOG_H
#define DIALOG_H

#include 
#include 

namespace Ui {
class Dialog;
}

class Dialog : public QDialog
{
    Q_OBJECT

public:
    explicit Dialog(QWidget *parent = 0);
    ~Dialog();

private slots:
    void OnBtMsgClicked();

  //  void ShowImage();

    void OnRbFruitCliced();


private:
    Ui::Dialog *ui;

    QImage m_BKImage;

    QButtonGroup* m_Image;

    uint m_CheckedId;
};

#endif // DIALOG_H
dialog.cpp:
#include "dialog.h"
#include "ui_dialog.h"
#include 
#include 

Dialog::Dialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dialog)
{
    ui->setupUi(this);

    m_CheckedId=-1;

    connect(ui->PB_IMAGE,SIGNAL(clicked() ),this,SLOT(OnBtMsgClicked()));

    m_Image=NULL;
    m_Image=new QButtonGroup(this);
    if(!m_Image)
        exit (1);
    m_Image->addButton(ui->RB_IMAGE1,0);
    m_Image->addButton(ui->RB_IMAGE2,1);


    connect(ui->RB_IMAGE1,SIGNAL(clicked() ),this,SLOT(OnRbFruitCliced()));
    connect(ui->RB_IMAGE2,SIGNAL(clicked() ),this,SLOT(OnRbFruitCliced()));

    connect(ui->PB_Photo,SIGNAL(clicked()),this,SLOT(OnBtMsgClicked()));
}

Dialog::~Dialog()
{
    delete ui;
}
void Dialog::OnBtMsgClicked()
{
    if(ui->RB_IMAGE1->isChecked())
    {
        QString fileName1 = QFileDialog ::getOpenFileName(this,tr("Open File"),
                                                             "",
                                                             tr("Images (*.png *.bmp *.jpg)"));
        if( ! m_BKImage.load(fileName1))
        {
            QMessageBox::warning(this,"warning","failes");
        }                                                           //需要去掉几百个像素,或者可以使用把lineedit : Bring to front,把label放在前面
        ui->LE_IMAGE1->setText(fileName1);
        ui->LB_IMAGE->resize( this->geometry().width() - 200,this->geometry().height() - 50);//设置宽高
        ui->LB_IMAGE->setPixmap( QPixmap::fromImage(m_BKImage.scaled(this->geometry().width() - 100,this->geometry().height(),Qt::KeepAspectRatio)));   //加载图片
        ui->LB_IMAGE->move( 0,0 );
    }
    else if(ui->RB_IMAGE2->isChecked())
    {
        QString fileName2 = QFileDialog ::getOpenFileName(this,tr("Open File"),
                                                             "",
                                                             tr("Images (*.png *.bmp *.jpg)"));
            if( ! m_BKImage.load(fileName2))
            {
                QMessageBox::warning(this,"warning","failes");
            }
           /* 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->LE_IMAGE2->setText(fileName2);
            ui->LB_IMAGE->resize( this->geometry().width() - 200,this->geometry().height() - 50);//设置宽高
            ui->LB_IMAGE->setPixmap( QPixmap::fromImage(m_BKImage.scaled(this->geometry().width() - 100,this->geometry().height(),Qt::KeepAspectRatio)));   //加载图片
            ui->LB_IMAGE->move( 0,0 );
    }
}
void Dialog::OnRbFruitCliced()
{
    m_CheckedId = m_Image->checkedId();
}

你可能感兴趣的:(2016年实训,函数,ui,对象,指针)