Qt之自定義QEvent

2021-09-30 20:08:11 字數 3084 閱讀 8619

2、如何定義個事件型別。qt中提供了[static] int qevent::registereventtype(int hint = -1)用來定義程式中唯一的事件型別。

4、postevent和sendevent的區別:

1)sendevent使用notify()函式直接給receiver傳送事件。(不支援多執行緒、event變數直接在堆疊上生成,在傳送事件時,不會刪除該事件)

2)postevent 向事件佇列中新增receiver和event,對應非同步處理事件。(支援多執行緒,event必須在堆上分配,因為post事件佇列將獲得事件的所有權,並在事件被post後刪除它)

5、自定義事件可以用來幹嘛。

1)用於物件間的通訊

#include "widget.h"

static qevent::type g_widget1_eventtype = static_cast(qevent::registereventtype());

static qevent::type g_widget2_eventtype = static_cast(qevent::registereventtype());

class myevent : public qevent

~myevent()

void setmessage(const qstring &msg)

qstring getmessage()const

private:

qstring m_message;

};class widget1:public qwidget

); qvboxlayout *mainlayout = new qvboxlayout;

mainlayout->addlayout(layout);

m_label = new qlabel("等待接收資訊...");

mainlayout->addwidget(m_label);

setlayout(mainlayout);

}void setrecevier(qobject *receiver)

protected:

bool event(qevent *event)

return qwidget::event(event);

}private:

qlabel *m_label;

qobject *m_receiver;

};class widget2:public qwidget

); qvboxlayout *mainlayout = new qvboxlayout;

mainlayout->addlayout(layout);

m_label = new qlabel("等待接收資訊...");

mainlayout->addwidget(m_label);

setlayout(mainlayout);

}void setrecevier(qobject *receiver)

protected:

bool event(qevent *event)

qwidget::event(event);

}private:

qlabel *m_label;

qobject *m_receiver;

};int main(int argc, char *ar**)

#include "main.moc"

2)多執行緒程式設計

static const qevent::type g_request_eventtype = static_cast(qevent::registereventtype());

static const qevent::type g_respond_eventtype = static_cast(qevent::registereventtype());

class downevent : public qevent

~downevent()

void setdata(const qstring data)

qstring getdata()const

void setprogress(int data)

int getprogress()const

void setreceiver(qobject *receiver)

qobject *getreceiver()

private:

qstring m_data;

int m_process;

qobject *m_receiver;

};class downloadthread : public qthread

~downloadthread()

}protected:

bool event(qevent *event)

return qthread::event(event);

}void run()

}private:

qstring m_data;

qobject *m_receiver;

};class widget : public qwidget

); mainlayout->addwidget(button);

m_bar = new qprogressbar;

connect(m_bar,&qprogressbar::valuechanged,[=](int value));

m_bar->setrange(0,100);

mainlayout->addwidget(m_bar);

setlayout(mainlayout);

}void setreceiver(qobject *receiver)

bool event(qevent *event)

return qwidget::event(event);

}private:

int m_process;

qobject *m_receiver;

qprogressbar *m_bar;

};int main(int argc, char *ar**)

#include "main.moc"

Qt練習之自定義時鐘

在qtproject中自學qt時,上面有乙個例項是編寫時鐘的,其中用到了qtimer,訊號和槽以及事件的知識,是練習qt的好例子,本人在例項上新增了秒鐘的設定,從而更加像乙個使用的時鐘錶了,下面展示一下執行結果。下面我來和大家分享一下,具體實現,類analogclock是自定義類,它繼承於qwidg...

qt 之自定義 搜尋框

當然,這些對於qt實現來說毫無壓力,只要思路清晰,分分鐘搞定。列舉 qlineedit actionposition常量值 描述qlineedit leadingposition 0當使用布局方向qt lefttoright時,部件顯示在文字左側,使用qt righttoleft則顯示在右側。qli...

Qt之QSS(Q PROPERTY 自定義屬性)

在qt之qss q property 原始屬性 一節我們分享了q property 原始屬性的用法,但通常情況下我們需要很多自定義樣式,例如 動態獲取qss樣式,這時自定義屬性就能很好的發揮其用武之地了。更多參考 在使用qabstractitemmodel qabstractitemdelegate...