前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >QT(三).电子相册(3)

QT(三).电子相册(3)

作者头像
franket
发布2021-09-14 14:11:46
1K0
发布2021-09-14 14:11:46
举报
文章被收录于专栏:技术杂记

会弹出一个小窗口

pic2.png
pic2.png

编译执行过程中没有报错,从结果来看,符合预期

进行操作检查,功能也相对完备


编译过程

从make的编译过程可以看出如下信息

代码语言:javascript
复制
[emacs@h102 pic]$ make 
/usr/local/Trolltech/Qt-4.8.6/bin/uic pic.ui -o ui_pic.h
g++ -c -pipe -O2 -Wall -W -D_REENTRANT -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -I/usr/local/Trolltech/Qt-4.8.6/mkspecs/linux-g++ -I. -I/usr/local/Trolltech/Qt-4.8.6/include/QtCore -I/usr/local/Trolltech/Qt-4.8.6/include/QtGui -I/usr/local/Trolltech/Qt-4.8.6/include -I. -I. -I. -o main.o main.cpp
g++ -c -pipe -O2 -Wall -W -D_REENTRANT -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -I/usr/local/Trolltech/Qt-4.8.6/mkspecs/linux-g++ -I. -I/usr/local/Trolltech/Qt-4.8.6/include/QtCore -I/usr/local/Trolltech/Qt-4.8.6/include/QtGui -I/usr/local/Trolltech/Qt-4.8.6/include -I. -I. -I. -o pic.o pic.cpp
/usr/local/Trolltech/Qt-4.8.6/bin/moc -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -I/usr/local/Trolltech/Qt-4.8.6/mkspecs/linux-g++ -I. -I/usr/local/Trolltech/Qt-4.8.6/include/QtCore -I/usr/local/Trolltech/Qt-4.8.6/include/QtGui -I/usr/local/Trolltech/Qt-4.8.6/include -I. -I. -I. pic.h -o moc_pic.cpp
g++ -c -pipe -O2 -Wall -W -D_REENTRANT -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -I/usr/local/Trolltech/Qt-4.8.6/mkspecs/linux-g++ -I. -I/usr/local/Trolltech/Qt-4.8.6/include/QtCore -I/usr/local/Trolltech/Qt-4.8.6/include/QtGui -I/usr/local/Trolltech/Qt-4.8.6/include -I. -I. -I. -o moc_pic.o moc_pic.cpp
g++ -Wl,-O1 -Wl,-rpath,/usr/local/Trolltech/Qt-4.8.6/lib -o pic main.o pic.o moc_pic.o    -L/usr/local/Trolltech/Qt-4.8.6/lib -lQtGui -L/usr/local/Trolltech/Qt-4.8.6/lib -L/usr/X11R6/lib -lQtCore -lpthread 
[emacs@h102 pic]$ echo $?
0
[emacs@h102 pic]$
  • 1.uic : pic.ui->ui_pic.h
  • 2.g++ : main.cpp->main.o
  • 3.g++ : pic.cpp->pic.o
  • 4.moc : pic.h->moc_pic.cpp
  • 5.g++ : moc_pic.cpp->moc_pic.o
  • 6.g++ : main.o pic.o moc_pic.o ->pic

生成关系

相互之间的生成关系为

代码语言:javascript
复制
pic.ui->ui_pic.h
main.cpp->main.o---------------|
pic.cpp->pic.o-----------------|->pic
pic.h->moc_pic.cpp->moc_pic.o--|

依赖关系

从源码中的依赖关系可知

代码语言:javascript
复制
[emacs@h102 pic]$ grep include main.cpp 
#include <QtGui/QApplication>
#include <QtCore/QTextCodec>
#include "pic.h"
[emacs@h102 pic]$ grep include pic.cpp 
#include <QDateTime>
#include <stdio.h>
#include "pic.h"
[emacs@h102 pic]$ grep include pic.h
#include <QDialog>
#include <QWidget>
#include <QTimer>
#include <QString>
#include "ui_pic.h"
[emacs@h102 pic]$

相互之间的依存关系为

代码语言:javascript
复制
pic.ui-[uic]->ui_pic.h    |->main.cpp-[g++]->main.o---------------|
                    |->pic.h-[moc]->moc_pic.cpp-[g++]->moc_pic.o--|->pic
                          |->pic.cpp-[g++]->pic.o-----------------|

*.pro 文件与 Makefile 文件的关系如下

代码语言:javascript
复制
source-[qmake -project]->*.pro-[qmake]->Makefile--|-[make]->target
    |---------------------------------------------|

ui_*.h

这个文件由 uic 根据 *.ui 生成的

我们来看看其中的内容

代码语言:javascript
复制
[emacs@h102 pic]$ cat ui_pic.h 
/********************************************************************************
** Form generated from reading UI file 'pic.ui'
**
** Created by: Qt User Interface Compiler version 4.8.6
**
** WARNING! All changes made in this file will be lost when recompiling UI file!
********************************************************************************/

#ifndef UI_PIC_H
#define UI_PIC_H

#include <QtCore/QVariant>
#include <QtGui/QAction>
#include <QtGui/QApplication>
#include <QtGui/QButtonGroup>
#include <QtGui/QDialog>
#include <QtGui/QGroupBox>
#include <QtGui/QHeaderView>
#include <QtGui/QLabel>
#include <QtGui/QLineEdit>
#include <QtGui/QPushButton>
#include <QtGui/QRadioButton>

QT_BEGIN_NAMESPACE

class Ui_Pic
{
public:
    QGroupBox *gbMode;
    QRadioButton *rbMan;
    QRadioButton *rbAuto;
    QGroupBox *gbMan;
    QPushButton *pbFirst;
    QPushButton *pbPre;
    QPushButton *pbNext;
    QPushButton *pbLast;
    QLabel *lbPic;
    QGroupBox *gbAuto;
    QLineEdit *le;
    QLabel *lbTime;

    void setupUi(QDialog *pic)
    {
        if (pic->objectName().isEmpty())
            pic->setObjectName(QString::fromUtf8("pic"));
        pic->setEnabled(true);
        pic->resize(656, 356);
        gbMode = new QGroupBox(pic);
        gbMode->setObjectName(QString::fromUtf8("gbMode"));
        gbMode->setEnabled(true);
        gbMode->setGeometry(QRect(530, 0, 111, 111));
        gbMode->setAutoFillBackground(false);
        gbMode->setFlat(false);
        gbMode->setCheckable(false);
        rbMan = new QRadioButton(gbMode);
        rbMan->setObjectName(QString::fromUtf8("rbMan"));
        rbMan->setGeometry(QRect(11, 30, 76, 23));
        rbAuto = new QRadioButton(gbMode);
        rbAuto->setObjectName(QString::fromUtf8("rbAuto"));
        rbAuto->setGeometry(QRect(11, 68, 55, 23));
        gbMan = new QGroupBox(pic);
        gbMan->setObjectName(QString::fromUtf8("gbMan"));
        gbMan->setEnabled(true);
        gbMan->setGeometry(QRect(530, 170, 111, 171));
        pbFirst = new QPushButton(gbMan);
        pbFirst->setObjectName(QString::fromUtf8("pbFirst"));
        pbFirst->setGeometry(QRect(11, 21, 77, 30));
        pbPre = new QPushButton(gbMan);
        pbPre->setObjectName(QString::fromUtf8("pbPre"));
        pbPre->setGeometry(QRect(11, 57, 77, 30));
        pbNext = new QPushButton(gbMan);
        pbNext->setObjectName(QString::fromUtf8("pbNext"));
        pbNext->setGeometry(QRect(11, 93, 77, 30));
        pbLast = new QPushButton(gbMan);
        pbLast->setObjectName(QString::fromUtf8("pbLast"));
        pbLast->setGeometry(QRect(11, 129, 77, 30));
        lbPic = new QLabel(pic);
        lbPic->setObjectName(QString::fromUtf8("lbPic"));
        lbPic->setGeometry(QRect(20, 40, 481, 301));
        lbPic->setAutoFillBackground(false);
        lbPic->setScaledContents(true);
        gbAuto = new QGroupBox(pic);
        gbAuto->setObjectName(QString::fromUtf8("gbAuto"));
        gbAuto->setEnabled(true);
        gbAuto->setGeometry(QRect(530, 110, 111, 61));
        le = new QLineEdit(gbAuto);
        le->setObjectName(QString::fromUtf8("le"));
        le->setGeometry(QRect(10, 20, 91, 25));
        lbTime = new QLabel(pic);
        lbTime->setObjectName(QString::fromUtf8("lbTime"));
        lbTime->setGeometry(QRect(20, 10, 271, 18));

        retranslateUi(pic);

        QMetaObject::connectSlotsByName(pic);
    } // setupUi

    void retranslateUi(QDialog *pic)
    {
        pic->setWindowTitle(QApplication::translate("Pic", "Dialog", 0, QApplication::UnicodeUTF8));
        gbMode->setTitle(QApplication::translate("Pic", "mode", 0, QApplication::UnicodeUTF8));
        rbMan->setText(QApplication::translate("Pic", "manual", 0, QApplication::UnicodeUTF8));
        rbAuto->setText(QApplication::translate("Pic", "auto", 0, QApplication::UnicodeUTF8));
        gbMan->setTitle(QApplication::translate("Pic", "manual", 0, QApplication::UnicodeUTF8));
        pbFirst->setText(QApplication::translate("Pic", "|<", 0, QApplication::UnicodeUTF8));
        pbPre->setText(QApplication::translate("Pic", "<", 0, QApplication::UnicodeUTF8));
        pbNext->setText(QApplication::translate("Pic", ">", 0, QApplication::UnicodeUTF8));
        pbLast->setText(QApplication::translate("Pic", ">|", 0, QApplication::UnicodeUTF8));
        lbPic->setText(QApplication::translate("Pic", "pic", 0, QApplication::UnicodeUTF8));
        gbAuto->setTitle(QApplication::translate("Pic", "auto", 0, QApplication::UnicodeUTF8));
        lbTime->setText(QApplication::translate("Pic", "now", 0, QApplication::UnicodeUTF8));
    } // retranslateUi

};

namespace Ui {
    class Pic: public Ui_Pic {};
} // namespace Ui

QT_END_NAMESPACE

#endif // UI_PIC_H
[emacs@h102 pic]$

头部注释中告诉我们这个文件是由 pic.ui 生成的

Qt UI 编译器的版本为 4.8.6

然后我们不要手动修改,因为重编译的过程中这些改动会丢失

需要注意的是,这里面定义了一个 Ui_Pic 类 , 这个类我们之前在 pic.h 中见过

代码语言:javascript
复制
class Pic: public QDialog,public Ui_Pic
{
  setupUi(this);
...
...
}

Pic 就是继承于 QDialogUi_Pic

通过这个文件的内容,可以看出,这个类在 public 中定义了各个控件的指针,然后定义了两个内联函数 setupUiretranslateUi

在 Pic 的构造函数中,首先就调用了 setupUi ,这个函数对控件的形象进行了配置


QDateTime

这个类用来提供日期和时间

The QDateTime class provides date and time functions

QDateTime 是 QDate 和 QTime 的组合,还提供各种对时间的计算与操作

A QDateTime object contains a calendar date and a clock time (a “datetime”). It is a combination of the QDate and QTime classes. It can read the current datetime from the system clock. It provides functions for comparing datetimes and for manipulating a datetime by adding a number of seconds, days, months, or years

QDateTime::currentDateTime

QDateTime QDateTime::currentDateTime () [static]

用来返回当前时间(作为系统时间返回的,以当前的时区)

Returns the current datetime, as reported by the system clock, in the local time zone

  • QDate::currentDate() : 返回日期
  • QTime::currentTime() : 返回时间
  • QDateTime::toTimeSpec : 返回秒数

QDateTime::toString

QString QDateTime::toString ( const QString & format ) const

以字符串的形式返回时间

Returns the datetime as a string. The format parameter determines the format of the result string

相关的格式可以参考官方API文档


QPixmap

继承自 QPaintDevice

QPixmap 用来保有图片信息

The QPixmap class is an off-screen image representation that can be used as a paint device

除了QPixmap ,Qt还有三种表示图片的类,QImage 、QBitmap 和 QPicture

  • QImage 经过了IO优化,是设计用来直接操作像素的
  • QPixmap 用来显示图片
  • QBitmap 继承自QPixmap 图个方便
  • QPicture 可以用来作画和绘图

Qt provides four classes for handling image data: QImage, QPixmap, QBitmap and QPicture. QImage is designed and optimized for I/O, and for direct pixel access and manipulation, while QPixmap is designed and optimized for showing images on screen. QBitmap is only a convenience class that inherits QPixmap, ensuring a depth of 1. The isQBitmap() function returns true if a QPixmap object is really a bitmap, otherwise returns false. Finally, the QPicture class is a paint device that records and replays QPainter commands

QPixmap::QPixmap

构造函数

QPixmap::QPixmap ( const QString & fileName, const char * format = 0, Qt::ImageConversionFlags flags = Qt::AutoColor )

通过文件名来构造

Constructs a pixmap from the file with the given fileName. If the file does not exist or is of an unknown format, the pixmap becomes a null pixmap

如果不指定图片格式,加载过程会根据文件的头部信息进行自动判断

The loader attempts to read the pixmap using the specified format. If the format is not specified (which is the default), the loader probes the file for a header to guess the file format

QLabel::setPixmap

这个函数可以将一个QPixmap 放到QLabel中进行显示

This property holds the label’s pixmap, If no pixmap has been set this will return 0


QRadioButton

继承自 QAbstractButton

单选按钮,在一组对象中,最多只能有一个单选按钮被选中,按钮的变化是自动排他的

The QRadioButton widget provides a radio button with a text label A QRadioButton is an option button that can be switched on (checked) or off (unchecked). Radio buttons typically present the user with a “one of many” choice. In a group of radio buttons only one radio button at a time can be checked; if the user selects another button, the previously selected button is switched off.

我们需要做的就是将它们放到一个组中

QAbstractButton::setChecked

代码语言:javascript
复制
bool isChecked () const 
void setChecked ( bool ) 

使对象处于选中状态

Only checkable buttons can be checked. By default, the button is unchecked


QButtonGroup

继承自QObject

这个类的对象可以提供一个容器将若干按钮组织起来

The QButtonGroup class provides a container to organize groups of button widgets

其主要的意义在于可以统一管理这一组对象的状态

QButtonGroup provides an abstract container into which button widgets can be placed. It does not provide a visual representation of this container (see QGroupBox for a container widget), but instead manages the states of each of the buttons in the group.

QRadioButtons 可以利用这个特性来进行排他选择

An exclusive button group switches off all checkable (toggle) buttons except the one that was clicked. By default, a button group is exclusive. The buttons in a button group are usually checkable QPushButton’s, QCheckBoxes (normally for non-exclusive button groups), or QRadioButtons. If you create an exclusive button group, you should ensure that one of the buttons in the group is initially checked; otherwise, the group will initially be in a state where no buttons are checked


QTimer

继承自 QObject

QTimer 提供重复的或一次性的计时器

The QTimer class provides repetitive and single-shot timers

通过 timeout() 信号和 start() 就可以重复触发槽函数

The QTimer class provides a high-level programming interface for timers. To use it, create a QTimer, connect its timeout() signal to the appropriate slots, and call start(). From then on it will emit the timeout() signal at constant intervals

代码语言:javascript
复制
     QTimer *timer = new QTimer(this);
     connect(timer, SIGNAL(timeout()), this, SLOT(update()));
     timer->start(1000);

timeout()

超时信号

void QTimer::timeout () [signal]

超时后触发信号

This signal is emitted when the timer times out

start()

void QTimer::start ( int msec ) [slot]

以指定的周期启动或重启计时器,单位是毫秒

Starts or restarts the timer with a timeout interval of msec milliseconds

stop()

void QTimer::stop () [slot]

停止定时器


总结

掌握如下概念对Qt的学习非常有用

  • 信号

如下类是Qt中常用的类,掌握它们的属性和方法很有必要

  • QDateTime
  • QPixmap
  • QRadioButton
  • QButtonGroup
  • QTimer

以下函数和宏非常重要

  • connect
  • SIGNAL
  • SLOT
  • Q_OBJECT

原文地址http://soft.dog/2017/02/26/qt-basic-03/

本文系转载,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文系转载前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 编译过程
    • 生成关系
      • 依赖关系
      • ui_*.h
      • QDateTime
        • QDateTime::currentDateTime
          • QDateTime::toString
          • QPixmap
            • QPixmap::QPixmap
              • QLabel::setPixmap
              • QRadioButton
                • QAbstractButton::setChecked
                • QButtonGroup
                • QTimer
                  • timeout()
                    • start()
                      • stop()
                      • 总结
                      相关产品与服务
                      容器服务
                      腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
                      领券
                      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档