1. 库的制作.
打开QtCreator, 选择, 选择文件->新建文件或项目->Library->C++库. 然后, 按照下面的步骤进行.
最后点击”完成”, 生成工程框架. 如下图所示
删除项目中的”libqbubble_global.h”, 向项目中添加实现文件. 最后修改”LibQBubble.h”, “LibQBubble.pro”文件.
其中, LibQBubble.pro内容如下:
#-------------------------------------------------
#
# Project created by QtCreator 2017-05-11T10:18:38
#
#-------------------------------------------------
QT += widgets
TARGET = LibQBubble
TEMPLATE = lib
SOURCES += LibQBubble.cpp \
MLabel.cpp \
GLabel.cpp
HEADERS += LibQBubble.h\
MLabel.h \
GLabel.h
MOC_DIR=temp/moc
RCC_DIR=temp/rcc
UI_DIR=temp/ui
OBJECTS_DIR=temp/obj
DESTDIR=lib
unix {
target.path = /usr/lib
INSTALLS += target
}
LibQBubble.h内容如下:
#ifndef LIBQBUBBLE_H
#define LIBQBUBBLE_H
#include <QListWidget>
#include <QtCore/qglobal.h>
class Q_DECL_EXPORT LibQBubble : public QListWidget
{
public:
enum PEOPLE{She,Me};
LibQBubble(QWidget *parent = 0);
void ShowContext(quint8 type, QString context, QString head_path);
void ShowText(QString text);
};
#endif // LIBQBUBBLE_H
其他文件内容略. 完整的工程框架如下:
最后, 重新构建工程, 将在lib目录下生成libLibQBubble.a和LibQBubble.dll. 至此, 库制作完成.
2. 库的应用
打开QtCreator, 利用向导创建名称为”Test_LibQBubble” 的”Widgets Application”应用程序. 生成工程框架 如下:
Test_LibQBubble.pro 内容如下:
#-------------------------------------------------
#
# Project created by QtCreator 2017-05-11T10:43:31
#
#-------------------------------------------------
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = Test_LibQBubble
TEMPLATE = app
SOURCES += main.cpp\
dialog.cpp
HEADERS += dialog.h
FORMS += dialog.ui
LIBS += -L$$PWD/3rdparty/lib/ -lLibQBubble
INCLUDEPATH += $$PWD/3rdparty/include
MOC_DIR=temp/moc
RCC_DIR=temp/rcc
UI_DIR=temp/ui
OBJECTS_DIR=temp/obj
DESTDIR=bin
RESOURCES += \
resource.qrc
dialog.h 文件内容如下:
#ifndef DIALOG_H
#define DIALOG_H
#include <QDialog>
#include "LibQBubble.h"
namespace Ui {
class Dialog;
}
class Dialog : public QDialog
{
Q_OBJECT
public:
explicit Dialog(QWidget *parent = 0);
~Dialog();
private:
Ui::Dialog *ui;
LibQBubble *bubble;
};
#endif // DIALOG_H
dialog.cpp 文件内容如下:
#include "dialog.h"
#include "ui_dialog.h"
#include <QHBoxLayout>
#include <QTime>
#include <QDate>
Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
ui->setupUi(this);
bubble = new LibQBubble(this);
bubble->show();
QHBoxLayout *layout = new QHBoxLayout(this);
layout->addWidget(bubble);
setLayout(layout);
QTime time = QTime::currentTime();
QDate date = QDate::currentDate();
QString dateTime = QString("%1 %2").arg(date.toString(Qt::ISODate))
.arg(time.toString(Qt::ISODate));
bubble->ShowText(dateTime);
bubble->ShowContext(LibQBubble::Me, "你好", ":/image/Me.bmp");
bubble->ShowContext(LibQBubble::She, "你好", ":/image/She.bmp");
bubble->ShowContext(LibQBubble::Me, "我们好像在哪里见过", ":/image/Me.bmp");
bubble->ShowContext(LibQBubble::She, "?", ":/image/She.bmp");
bubble->ShowContext(LibQBubble::Me, "我们好像在哪里见过你记得吗? 好像那是一个秋天夕阳西下, "
"你美得让我不敢和你说话, 你经过我时风起浮动我的发", ":/image/Me.bmp");
bubble->ShowContext(LibQBubble::She, "银杏还是会黄的, 雪也会再次染白整个世界. 安好", ":/image/She.bmp");
}
Dialog::~Dialog()
{
delete ui;
}
在工程目录下创建”3rdparty/lib”和”3rdparty/include”. 将”LibQBubble.h”文件复制到”3rdparty/include”目录下, 将”LibQBubble.dll”, “libLibQBubble.a”复制到”3rdparty/lib”目录下.
最后, 构建运行如下