1. 程式人生 > >Qt 中的訊號與槽,連線日誌庫QsLog,在介面上顯示出日誌資訊

Qt 中的訊號與槽,連線日誌庫QsLog,在介面上顯示出日誌資訊

新建一個基於QWidget的工程,在介面上新增一個QTextBrowser控制元件,用來在介面顯示日誌資訊。

在pro檔案中新增QsLog的標頭檔案和lib庫

INCLUDEPATH += include/QsLog.h \
               include/QsLogDest.h \
               include/QsLogDestConsole.h \
               include/QsLogDestFile.h \
               include/QsLogDestFunctor.h \
               include/QsLogDisableForThisFile.
h \ include/QsLogLevel.h LIBS += $$PWD/lib/QsLog2.lib

在widget.h中新增槽函式:

public slots:
    void logSlot(const QString &message, int level);

widget.h檔案:

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>

namespace Ui {
class Widget;
}

class Widget : public QWidget
{
    Q_OBJECT

public:
explicit Widget(QWidget *parent = 0); ~Widget(); void initLogger(); void destroyLogger(); public slots: void logSlot(const QString &message, int level); private: Ui::Widget *ui; }; #endif // WIDGET_H

widget.cpp檔案:

#include "widget.h"
#include "ui_widget.h"

#include "include/QsLog.h"
#include "include/QsLogDest.h" #include <QCoreApplication> #include <QDir> //#include <iostream> using namespace QsLogging; /* void logFunction(const QString &message, QsLogging::Level level) { std::cout << "From log function: " << qPrintable(message) << " " << static_cast<int>(level) << std::endl; } */ Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget) { ui->setupUi(this); initLogger(); } Widget::~Widget() { delete ui; destroyLogger(); } void Widget::initLogger() { // 1. init the logging mechanism Logger& logger = Logger::instance(); logger.setLoggingLevel(QsLogging::TraceLevel); //設定log位置 const QString sLogPath(QDir(QCoreApplication::applicationDirPath()).filePath("log.txt")); // 2. add two destinations DestinationPtr fileDestination(DestinationFactory::MakeFileDestination( sLogPath, EnableLogRotation, MaxSizeBytes(512), MaxOldLogCount(2))); DestinationPtr debugDestination(DestinationFactory::MakeDebugOutputDestination()); //DestinationPtr functorDestination(DestinationFactory::MakeFunctorDestination(&logFunction)); //這樣和槽函式連線 DestinationPtr sigsSlotDestination(DestinationFactory::MakeFunctorDestination(this, SLOT(logSlot(QString,int)))); logger.addDestination(debugDestination); logger.addDestination(fileDestination); //logger.addDestination(functorDestination); logger.addDestination(sigsSlotDestination); // 3. start logging QLOG_INFO() << "Program started"; QLOG_INFO() << "Built with Qt" << QT_VERSION_STR << "running on" << qVersion(); QLOG_TRACE() << "Here's a" << QString::fromUtf8("trace") << "message"; QLOG_DEBUG() << "Here's a" << static_cast<int>(QsLogging::DebugLevel) << "message"; QLOG_WARN() << "Uh-oh!"; qDebug() << "This message won't be picked up by the logger"; QLOG_ERROR() << "An error has occurred"; qWarning() << "Neither will this one"; QLOG_FATAL() << "Fatal error!"; } void Widget::destroyLogger() { QsLogging::Logger::destroyInstance(); } void Widget::logSlot(const QString &message, int level) { ui->textBrowser->append(qPrintable(message)); }

執行效果:
在這裡插入圖片描述
檢視函式呼叫過程:

DestinationPtr DestinationFactory::MakeFunctorDestination(QsLogging::Destination::LogFunction f)
{
    return DestinationPtr(new FunctorDestination(f));
}

stinationPtr DestinationFactory::MakeFunctorDestination(QObject *receiver, 
														const char *member)
{
    return DestinationPtr(new FunctorDestination(receiver, member));
}

繼續看FunctorDestination的建構函式:
只有兩個建構函式

  	explicit FunctorDestination(LogFunction f);
    FunctorDestination(QObject *receiver, const char *member);

第二個建構函式,找到了連線訊號與槽的地方,這就是我們需要的函式。

QsLogging::FunctorDestination::FunctorDestination(QObject *receiver, const char *member)
    : QObject(NULL)
    , mLogFunction(NULL)
{
    connect(this, SIGNAL(logMessageReady(QString,int)), receiver, member, Qt::QueuedConnection);
}

相關推薦

Qt 訊號連線日誌QsLog介面顯示日誌資訊

新建一個基於QWidget的工程,在介面上新增一個QTextBrowser控制元件,用來在介面顯示日誌資訊。 在pro檔案中新增QsLog的標頭檔案和lib庫 INCLUDEPATH += include/QsLog.h \ inc

Qt 筆記:訊號連線方式

深入訊號與槽的連線方式 -Qt::DirectConnection(立即呼叫) -Qt::QueuedConnection(非同步呼叫) -Qt::BlockingQueuedConnection(同步呼叫) -Qt::AutoConnection(預設連線) -Q

QT 訊號連線 未響應

日常錯誤記錄 在連線訊號和槽時 訊號和槽的引數要一一對應,名字空間也要對應 錯誤: connect(p_topShapeMatchThread,SIGNAL(send_Result(HalconCp

QT使用訊號注意事項

QT使用訊號與槽注意事項 如需轉載請標明出處:http://blog.csdn.net/itas109 QQ技術交流群:129518033 目錄 文章目錄 QT使用訊號與槽注意事項 @[toc] 前言

pyqt4不同類之間訊號進行連線

這個部落格是自己在使用pyqt4過程中,解決具體的總結,僅供個人備忘。 不同類之間訊號與槽的對應,需要三個步驟: 1)在類A裡定義發射訊號,使用emit()方法,發射訊號A_signal(); 2)在類B裡定義槽函式B_slot(); 3)在主函式main()裡,使用

PyQt訊號之多執行緒訊號的使用(六)

簡單多執行緒訊號與槽的使用 最簡單的多執行緒使用方法是利用QThread函式,展示QThread函式和訊號簡單結合的方法 import sys from PyQt5.QtCore import

Qt學習之路_3(VS下Qt訊號初次體驗)

在Qt中是使用訊號與槽的機制來完成事件的響應過程的。網上Qt的開發基於Qt Creator的資料比較多,基於vs下的資料除了其環境配置方面的外就剩下很少了。開始以為2者環境下的開發方式相同,後面稍微接觸了下發現還是有微妙的區別的,Qt在vs下畢竟是add-in嵌入的,用起

Qt訊號的工作機制

void QMetaObject::activate(QObject *sender, const QMetaObject *m, int local_signal_index, void **argv) { activate(sender, QM

Qt訊號函式的3種對映方法

在Qt Creater中提供了三種建立訊號和槽的方法,詳細介紹如下: 實現功能:點選OK按鈕,TextLabel顯示“Hello World” 詳細過程: 首先,執行Qt Creater,建立Qt Gui application工程。 其次,在點選.ui檔案,在控制元件欄

Qt5 訊號的新寫法

Qt4中最常用的訊號槽寫法: connect(obj1, SIGNAL(fun1(param1, param2,...)), obj2, SLOT(fun2(param1,...))); //編譯後 connect(obj1, "fun1(param1, param2,

Qt訊號事件的小結

這幾天在使用Qt的時候,遇到一些關於訊號、事件的一些問題。發現對訊號,事件的區別不是很瞭解。於是上網找了一些資料,做了一點筆記總結下,方便自己也方便他人。要是有不對的地方,還望大家指出,大家一起進步

Qt訊號你可能不知道的那些

說到訊號與槽,這是Qt獨有的特點。 1、應該知道的: 一般用訊號和槽都會用到:signals和slots Qt4用法:     connect(sender, SIGNAL(signal), rece

Qt訊號(純乾貨)

       接觸Qt斷斷續續有些時間了,總看了一堆的文章說訊號槽的概念,心裡就想罵人,做為一個初學者,最重要的就是怎麼寫程式碼,寫程式碼寫多了,再去看理論,有時水到渠成的就明白那些理論了。但所有講訊號槽的都把一堆訊號槽的好處說一通,把MFC的訊息機制貶一通。具體程式碼

QT多執行緒物件訊號連線的解決辦法

1、在接收者建立執行緒中,把接收者移動到主執行緒中: pReceiverObj->moveToThread(QApplication::instance()->thread()); 2、這樣傳送訊號的時候,就會在主執行緒事件佇列處理中來處理了。 把connect的最

qt訊號斷開連線

在qt中使用訊號槽時,有時會想中途斷開訊號槽的連線,接下來將呈現四種連線斷開方法: one:斷開明確指定接受物件訊號槽,如果連線斷開成功返回true,否則返回false bool QObject::disconnect(const QObject *sender, con

QT 訊號在不同執行緒不能連線的問題

QObject::connect: Cannot queue arguments of type 'ERROR_LEVEL'  (Make sure 'ERROR_LEVEL' is registered using qRegisterMetaType().) 其中ER

QtQt訊號使用不當使程式崩潰

問題描述 跨執行緒使用Qt訊號和槽,訊號傳送時間間隔大於槽函式處理時間時,造成程式崩潰。 原因分析 跨執行緒使用Qt訊號和槽時,connect預設是QueuedConnection,佇列連線方式。 訊號傳遞給槽函式的引數,分配記憶體後放入佇列,如果槽

關於Qt繼承了QObject類的訊號問題:

在QT開發中,如果你要在自己實現的類中用訊號或者槽函式,你一定遇到過如下問題:undefined reference to vtable for ** 即使你繼承了QObject,即使你添加了Q_Object...... 廢話不說,直接告訴你解決辦法:執行“構建”中的

關於Qt訊號機制的一些問題

一、connect函式的引數在連線訊號與槽的函式connect中,connect函式的最後一個引數type可以指定傳遞訊號的方式,它是Qt::ConnectionType列舉型別常量。有五種型別。Qt::AutoConnection   當訊號傳送者和接收者處於同一執行緒內時

QT學習day02---圖形介面、對話方塊、訊號

一、QT圖形介面 ①QT座標軸 起點:左上角 X:橫軸 Y:縱軸 ②QWidget、QDialog、QMainWindow的異同點 QWidget:視窗為空,什麼內容都沒有 QMainWindow:含有選單欄,狀態列,工具欄等…已經包含了QWidget QDialog:對話方塊(