久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

將外部事件循環(huán)與 Qt 結(jié)合

Combing an External Event Loop with Qt#39;s(將外部事件循環(huán)與 Qt 結(jié)合)
本文介紹了將外部事件循環(huán)與 Qt 結(jié)合的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我正在為開源客戶端/服務器 4X 策略游戲 Thousand Parsec 構(gòu)建 Qt 客戶端.這是一個 Google Summer of Code 項目.然而,我陷入了死胡同.基本上,客戶端通過促進客戶端/服務器通信的 C++ 協(xié)議層與服務器接口.該協(xié)議的文檔可在此處獲得.

現(xiàn)在我的問題是協(xié)議要求您創(chuàng)建虛擬 EventLoop 類的子類 (link) 在您的客戶端中.有一個示例 SimpleEventLoop 用于同一鏈接上的控制臺客戶端.我很難弄清楚如何設(shè)計我自己的事件循環(huán)子類來處理協(xié)議的事件,同時連接到 Qt 應用程序.我的研究讓我相信 QAbstractEventDispatcher 是我想使用的 Qt 類但文檔似乎很薄,我不確定我將如何做這件事.

有沒有其他人有將外部事件循環(huán)與 Qt 應用程序聯(lián)系起來的經(jīng)驗?我還在 Qt 頁面上找到了這個 example 但是這不是很有幫助 - 或者至少我沒有真正理解它.

謝謝!

解決方案

我最近沒有做太多的 Qt 開發(fā),但是如果我沒記錯的話,可以調(diào)用 QApplication::processEvents() 在您自己的事件循環(huán)中(而不是通過 QApplication::exec())

我利用周日上午緩慢的機會試駕/了解了一些關(guān)于PyQt(Qt 的 Python 綁定)并在下面拼湊了一個概念驗證代碼.使用基于 QApplication::processEvents() 的自定義事件循環(huán)替換對 QApplication::exec() 的調(diào)用似乎可行.>

我也快速查看了simpleeventloop.cpptpclient-cpptext main.cpp/a>.從它的外觀來看,只需在 SimpleEventLoop::runEventLoop() 的主循環(huán)中的某處添加 QApplication::processEvents() 應該沒問題.要將其添加到主循環(huán)中,我可能會替換 select() 函數(shù)的 tv 間隔/gitweb/gitweb.cgi?p=libtpproto-cpp.git;a=blob;f=tpproto/simpleeventloop.cpp;hb=ebfc08b322c552d73d34a368cca0623782f8d3f8#l106" rel="nofollines">code>r1117

tv.tv_sec = 0;tv.tv_usec = 10000;//每 0.01 秒運行一次 processEvents()app->processEvents();

并更改 行 89void SimpleEventLoop::runEventLoop(QApplication *app).將您常用的 Qt 內(nèi)容添加到您的客戶端實現(xiàn)中應該沒問題(您替換 tpclient-cpptext main.cpp)

不過看起來像黑客.我可能會從這樣的事情開始.我認為你的想法包裝 TPSocket 和 Qt 各自概念中的計時器,以便使用 QAbstractEventDispatcherQEventLoop 是更好的長期解決方案.然后,您的 runEventLoop() 只需調(diào)用 QApplication::exec() 就足夠了.但我以前從未使用過 QAbstractEventDispatcher,所以請采納我的意見.

導入系統(tǒng)導入時間從 PyQt4 導入 QtGui從 PyQt4 導入 QtCore# 全局變量用作通知我的快速而骯臟的方式# MainWindow 已經(jīng)退出的主事件循環(huán)APP_RUNNING = 假類 SampleMainWindow(QtGui.QMainWindow):def __init__(self, parent=None):QtGui.QMainWindow.__init__(self)全球APP_RUNNINGAPP_RUNNING = 真# 主窗口self.setGeometry(300, 300, 250, 150)self.setWindowTitle('測試')self.statusBar().showMessage('Ready')# 退出動作(假設(shè)退出圖標來自# http://upload.wikimedia.org/wikipedia/commons/b/bc/Exit.png# 保存為 Exit.png 與此文件在同一文件夾中)exitAction = QtGui.QAction(QtGui.QIcon('Exit.png'),'出口',自己)exitAction.setShortcut('Ctrl+Q')exitAction.setStatusTip('退出應用程序')self.connect(exitAction),QtCore.SIGNAL('觸發(fā)()'),QtCore.SLOT('close()'))# 主菜單菜單欄 = self.menuBar()fileMenu = menubar.addMenu('&File')fileMenu.addAction(exitAction)# 工具欄self.toolbar = self.addToolBar('退出')self.toolbar.addAction(exitAction)# 文本編輯器textEdit = QtGui.QTextEdit()self.setCentralWidget(textEdit)#工具提示textEdit.setToolTip('輸入一些文字')QtGui.QToolTip.setFont(QtGui.QFont('English', 12))def closeEvent(self, event):回復 = QtGui.QMessageBox.question(self,'信息',你確定嗎?",QtGui.QMessageBox.Yes,QtGui.QMessageBox.No)如果回復 == QtGui.QMessageBox.Yes:事件.接受()全球APP_RUNNINGAPP_RUNNING = 假別的:事件.忽略()# 主程序app = QtGui.QApplication(sys.argv)testWindow = SampleMainWindow()testWindow.show()# 運行自定義事件循環(huán)而不是 app.exec_()當 APP_RUNNING 時:app.processEvents()# sleep 以防止我的偉大的"事件循環(huán)消耗 100% cpu時間.睡眠(0.01)

I'm building a Qt client for the open-source client/server 4X strategy game Thousand Parsec. This a Google Summer of Code project. I'm however stuck at a dead end. Basically, the client interfaces with the server via a C++ protocol layer that facilitates client/server communication. The protocol's documentation is available here.

Now my problem is that the protocol requires you to create a subclass of the virtual EventLoop class (link) in your client. There is an example SimpleEventLoop used for console clients on the same link. I'm having difficulty figuring out how I can design my own event loop subclass that handles the protocol's events while hooking into the Qt application at the same time. My research has lead me to believe QAbstractEventDispatcher is the Qt class I want to use but the documentation seems pretty slim and I'm not exactly sure how I would go about doing this.

Does anyone else have experience linking external event loops with a Qt application? I've also found this example on the Qt page but it wasn't very helpful - or at least I didn't really understand it.

Thanks!

解決方案

I haven't done too much Qt development recently, but if I remember correctly, you can call QApplication::processEvents() within your own event loop (instead of starting the Qt main loop through QApplication::exec())

Edit: I have used the opportunity of a slow Sunday morning to test-drive / learn something about PyQt (Python bindings for Qt) and cobbled together a proof-of-concept code below. Replacing the call to QApplication::exec() with a custom event loop based on QApplication::processEvents() seems to work.

I have also quickly looked at simpleeventloop.cpp and tpclient-cpptext main.cpp. From the looks of it, it shoud be fine to just add QApplication::processEvents() somewhere in the main loop of SimpleEventLoop::runEventLoop(). To add it to the main loop, I would probably replace the tv interval for the select() function in lines 106 through 117 with

tv.tv_sec = 0;
tv.tv_usec = 10000;   // run processEvents() every 0.01 seconds
app->processEvents();

and change the signature in line 89 to void SimpleEventLoop::runEventLoop(QApplication *app). It should than be fine to add your usual Qt stuff to your implementation of the client (your replacement of tpclient-cpptext main.cpp)

Looks like a hack, though. I would probably start with something like this to get started. I think that your idea of wrapping TPSocket and the timer within Qt's respective concepts in order to forward them with the QAbstractEventDispatcher to the QEventLoop is the better long-term solution. It should then be sufficient that your runEventLoop() simply calls QApplication::exec(). But I have never used QAbstractEventDispatcher before, so take my comments for what they are.

import sys
import time

from PyQt4 import QtGui
from PyQt4 import QtCore

# Global variable used as a quick and dirty way to notify my
# main event loop that the MainWindow has been exited
APP_RUNNING = False

class SampleMainWindow(QtGui.QMainWindow):
    def __init__(self, parent=None):
        QtGui.QMainWindow.__init__(self)
        global APP_RUNNING
        APP_RUNNING = True

        # main window
        self.setGeometry(300, 300, 250, 150)
        self.setWindowTitle('Test')
        self.statusBar().showMessage('Ready')

        # exit action (assumes that the exit icon from
        # http://upload.wikimedia.org/wikipedia/commons/b/bc/Exit.png
        # is saved as Exit.png in the same folder as this file)
        exitAction = QtGui.QAction(QtGui.QIcon('Exit.png')
                                   ,'Exit'
                                   ,self)
        exitAction.setShortcut('Ctrl+Q')
        exitAction.setStatusTip('Exit application')
        self.connect(exitAction
                     ,QtCore.SIGNAL('triggered()')
                     ,QtCore.SLOT('close()'))

        # main menu
        menubar = self.menuBar()
        fileMenu = menubar.addMenu('&File')
        fileMenu.addAction(exitAction)

        # toolbar
        self.toolbar = self.addToolBar('Exit')
        self.toolbar.addAction(exitAction)

        # text editor
        textEdit = QtGui.QTextEdit()
        self.setCentralWidget(textEdit)

        #tool tip
        textEdit.setToolTip('Enter some text')
        QtGui.QToolTip.setFont(QtGui.QFont('English', 12))

    def closeEvent(self, event):
        reply = QtGui.QMessageBox.question(self
                                           ,'Message'
                                           ,"Are you sure?"
                                           ,QtGui.QMessageBox.Yes
                                           ,QtGui.QMessageBox.No)

        if reply == QtGui.QMessageBox.Yes:
            event.accept()
            global APP_RUNNING
            APP_RUNNING = False
        else:
            event.ignore()

# main program
app = QtGui.QApplication(sys.argv)
testWindow = SampleMainWindow()
testWindow.show()
# run custom event loop instead of app.exec_()
while APP_RUNNING:
    app.processEvents()
    # sleep to prevent that my "great" event loop eats 100% cpu
    time.sleep(0.01)

這篇關(guān)于將外部事件循環(huán)與 Qt 結(jié)合的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

How can I read and manipulate CSV file data in C++?(如何在 C++ 中讀取和操作 CSV 文件數(shù)據(jù)?)
In C++ why can#39;t I write a for() loop like this: for( int i = 1, double i2 = 0; (在 C++ 中,為什么我不能像這樣編寫 for() 循環(huán): for( int i = 1, double i2 = 0;)
How does OpenMP handle nested loops?(OpenMP 如何處理嵌套循環(huán)?)
Reusing thread in loop c++(在循環(huán) C++ 中重用線程)
Precise thread sleep needed. Max 1ms error(需要精確的線程睡眠.最大 1ms 誤差)
Is there ever a need for a quot;do {...} while ( )quot; loop?(是否需要“do {...} while ()?環(huán)形?)
主站蜘蛛池模板: 亚洲精品视频免费 | 久久国产欧美日韩精品 | 偷拍亚洲色图 | 超碰精品在线观看 | 欧美亚洲激情 | 男女污网站 | 国产一区欧美 | 亚洲精品久久久久中文字幕欢迎你 | 国产精品综合 | 久久蜜桃资源一区二区老牛 | 在线观看视频h | 国产a视频 | 久久久久国产精品 | 国产精品一区二区久久精品爱微奶 | 欧美456| 日韩成人在线观看 | 久久成人一区 | 精品人伦一区二区三区蜜桃网站 | 亚洲三区在线 | 99爱在线免费观看 | 国产精品久久久久久久久免费 | 一区二区精品视频 | 91精品久久久久久久久中文字幕 | 免费艹逼视频 | 欧美综合一区二区三区 | 欧美三级视频在线观看 | 午夜精品一区二区三区在线播放 | 狠狠爱视频 | 国产精品777一区二区 | 视频一区在线观看 | 亚洲成人久久久 | 日韩一区二区三区在线观看 | 日韩一区二区在线播放 | 91aiai | 久久精品亚洲精品国产欧美 | 国产精品国产a级 | 欧美日韩综合 | 免费1区2区3区 | 三级视频在线观看电影 | 成人18亚洲xxoo | 亚洲精品久久久久久久久久久 |