pyqt QObject:无法为不同线程中的父级创建子级

时间:2012-06-05 22:37:04

标签: python pyqt qthread

QObject:无法为不同线程中的父级创建子级。 (Parent是QTextDocument(0x9919018),父线程是QThread(0x97331e0),当前线程是flooderthread(0x97b4c10) 错误的意思?很抱歉,因为我是pyqt的新手 这是代码:

我知道代码已经完成但它应该可以工作我想问题是myfun.log函数...

    #! /usr/bin/python
# -*- coding: utf-8 -*-
import urllib, urllib2, itertools, threading, cookielib, Cookie, sys, time, hashlib, os
from PyQt4 import QtCore, QtGui
try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    _fromUtf8 = lambda s: s
gui=QtGui.QApplication.processEvents
texttoset=""
class fun():
    global texttoset
    def checkpassword(self):
        if ui.passwordcheck.isChecked()==True:
            return 1
        else :
            return 0
    def log(self, text):
        if text != False:
            firsttext=str(ui.console.toPlainText())
            secondtext=firsttext+text+"\n"
            ui.console.setText(secondtext)
            log=open("log.log", "a")
            log.write(text+"\n")
            log.close()
        else :
            firsttext=str(ui.console.toPlainText())
            secondtext=firsttext+texttoset+"\n"
            ui.console.setText(secondtext)
            log=open("log.log", "a")
            log.write(texttoset+"\n")
            log.close()
    def disable(self):
        MainWindow.setEnabled(False)
        pass
    def enable(self):
        MainWindow.setEnabled(True)
        pass
    def checkmethod(self):
        if ui.get.isChecked()==True:
            return 1
        elif ui.post.isChecked()==True:
            return 2
        else :
            return 0
    def main(self):
        connecter()
        gui()
        f1.start()
        gui()
        time.sleep(3)
        gui()
        f2.start()
        gui()
        time.sleep(3)
        gui()
        f3.start()
        gui()
        time.sleep(3)
        gui()
        f4.start()
        gui()
        time.sleep(3)
        gui()
        f5.start()
        gui()
        self.sleep(3)
        gui()
        f6.start()
        gui()
    def killer(self):
        f1.terminate()
        f2.terminate()
        f3.terminate()
        f4.terminate()
        f5.terminate()
        f6.terminate()
    def close(self):
        self.killer()
        os.abort()
        sys.exit()

myfun=fun()
def connecter():
    QtCore.QObject.connect(f1, QtCore.SIGNAL("log(bool)"), myfun.log)
    QtCore.QObject.connect(f1, QtCore.SIGNAL("enable()"), myfun.enable)
    QtCore.QObject.connect(f1, QtCore.SIGNAL("disable()"), myfun.disable)

    QtCore.QObject.connect(f2, QtCore.SIGNAL("log(bool)"), myfun.log)
    QtCore.QObject.connect(f2, QtCore.SIGNAL("enable()"), myfun.enable)
    QtCore.QObject.connect(f2, QtCore.SIGNAL("disable()"), myfun.disable)


    QtCore.QObject.connect(f3, QtCore.SIGNAL("log(bool)"), myfun.log)
    QtCore.QObject.connect(f3, QtCore.SIGNAL("enable()"), myfun.enable)
    QtCore.QObject.connect(f3, QtCore.SIGNAL("disable()"), myfun.disable)


    QtCore.QObject.connect(f4, QtCore.SIGNAL("log(bool)"), myfun.log)
    QtCore.QObject.connect(f4, QtCore.SIGNAL("enable()"), myfun.enable)
    QtCore.QObject.connect(f4, QtCore.SIGNAL("disable()"), myfun.disable)


    QtCore.QObject.connect(f5, QtCore.SIGNAL("log(bool)"), myfun.log)
    QtCore.QObject.connect(f5, QtCore.SIGNAL("enable()"), myfun.enable)
    QtCore.QObject.connect(f5, QtCore.SIGNAL("disable()"), myfun.disable)


    QtCore.QObject.connect(f6, QtCore.SIGNAL("log(bool)"), myfun.log)
    QtCore.QObject.connect(f6, QtCore.SIGNAL("enable()"), myfun.enable)
    QtCore.QObject.connect(f6, QtCore.SIGNAL("disable()"), myfun.disable)
x=0
num=0
class flooderthread(QtCore.QThread):
    global texttoset
    def __init__(self, x, num):
        QtCore.QThread.__init__(self)
        self.x=x
        self.num=num
    def log(self, text):
        texttolog=str(text)
        time.sleep(1)
        self.emit(QtCore.SIGNAL("log(bool)"), False)
        time.sleep(2)
    def enable(self):
        time.sleep(1)
        self.emit(QtCore.SIGNAL("enable()"))
    def disable(self):
        time.sleep(1)
        self.emit(QtCore.SIGNAL("disable()"))
    def run(self):
        connecter()
        self.log("\n\n--------------------------------------------------new session-------------------------------------\n\n")
        itered=False
        gui()
        self.disable()
        gui()
        self.log("setting params...")
        param={ui.dataname1.text():ui.datavalue1.text(),ui.dataname3.text():ui.datavalue3.text(),ui.dataname3.text():ui.datavalue3.text(), }
        self.log("checking password...")
        if myfun.checkpassword()==1:
            itered=True
            self.log("password is true")
        else :
            self.log("password is null ")
            self.log("itered operation")
        self.log("setting url")
        url=str(ui.url.text())
        if url[:4]!="http" and url[:3]!="ftp":
            self.log("url error exiting the whole function")
            self.log("please set a valide protocole!!")
            gui()
            self.enable()
            gui()
            return 1
            pass
        else :
            self.log("valid url")
            gui()
            self.log("url is "+url)
        self.log("setting proxy")
        proxy="http://"+ui.proxyuser.text()+":"+ui.proxypass.text()+"@"+ui.proxyhost.text()+":"+ui.proxyport.text()
        self.log("proxy is "+proxy)
        gui()
        self.log("preparing params...")
        urlparam=urllib.urlencode(param)
        gui()
        self.log("params are "+urlparam)
        self.log("setting up headers...")
        header={'User-Agent':str(ui.useragent.toPlainText())}
        self.log("headers are "+ str(header))
        self.log("setting up proxy handler..")
        proxyhandler=urllib2.ProxyHandler({"http":str(proxy)})
        self.log("checking method")
        if myfun.checkmethod()==1:
            self.log("method is get..")
            self.log("setting request..")
            finalurl=url+urlparam
            gui()
            self.log("final url is"+finalurl)
            req=urllib2.Request(finalurl, None, headers)
        elif myfun.checkmethod()==2:
            self.log("method is post...")
            self.log("setting request..")
            finalurl=url
            gui()
            self.log("final url is "+finalurl)
            req=urllib2.Request(finalurl, urlparam, header)
        else :
            self.log("error has been accourded")
            self.log("please select a method!!")
            gui()
            self.log("exiting the whole functions")
            gui()
            self.enable()
            return 1
            pass
        self.log("intilizing cookies..")
        c1=Cookie.SimpleCookie()
        c1[str(ui.cookiename1.text())]=str(ui.cookievalue1.text())
        c1[str(ui.cookiename1.text())]['path']='/'
        c1[str(ui.cookiename2.text())]=str(ui.cookievalue2.text())
        c1[str(ui.cookiename2.text())]['path']='/'
        c1[str(ui.cookiename3.text())]=str(ui.cookievalue3.text())
        c1[str(ui.cookiename3.text())]['domain']=url
        c1[str(ui.cookiename3.text())]['path']='/'
        c1[str(ui.cookiename4.text())]=str(ui.cookievalue4.text())
        c1[str(ui.cookiename4.text())]['domain']=url
        c1[str(ui.cookiename4.text())]['path']='/'
        self.log("cookies are.. :"+str(c1))
        cj=cookielib.CookieJar()
        cj.set_cookie(c1)
        opener = urllib2.build_opener(proxyhandler, urllib2.HTTPCookieProcessor(cj))
        self.log("insatlling opener")
        urllib2.install_opener(opener)
        self.log("setting the two operations....")
        if itered==Fasle:
            self.log("starting the flooding loop")
            gui()
            while true:
                try:
                    gui()
                    opener.open(req)
                except e:
                    self.log("error connecting : "+e.reason)
                    self.log("will continue....")
                    continue
                gui()

        elif itered==True:
            pass
f1=flooderthread(1, 1)
f2=flooderthread(2, 2)
f3=flooderthread(3, 3)
f4=flooderthread(4, 4)
f5=flooderthread(5, 5)
f6=flooderthread(6, 6)
class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName(_fromUtf8("MainWindow"))
        MainWindow.setMinimumSize(QtCore.QSize(838, 500))
        MainWindow.setMaximumSize(QtCore.QSize(838, 500))
        MainWindow.setWindowTitle(QtGui.QApplication.translate("MainWindow", "memo flooder", None, QtGui.QApplication.UnicodeUTF8))
        self.centralwidget = QtGui.QWidget(MainWindow)
        self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
        self.console=QtGui.QTextEdit(self.centralwidget)
        self.console.setGeometry(10, 350, 800,130)
        self.console.setReadOnly(True)
        self.console.setObjectName("console")
        self.groupBox = QtGui.QGroupBox(self.centralwidget)
        self.groupBox.setGeometry(QtCore.QRect(30, 50, 71, 80))
        self.groupBox.setTitle(QtGui.QApplication.translate("MainWindow", "method:", None, QtGui.QApplication.UnicodeUTF8))
        self.groupBox.setObjectName(_fromUtf8("groupBox"))
        self.post = QtGui.QRadioButton(self.groupBox)
        self.post.setGeometry(QtCore.QRect(10, 20, 61, 22))
        self.post.setText(QtGui.QApplication.translate("MainWindow", "post", None, QtGui.QApplication.UnicodeUTF8))
        self.post.setChecked(True)
        self.post.setObjectName(_fromUtf8("post"))
        self.get = QtGui.QRadioButton(self.groupBox)
        self.get.setGeometry(QtCore.QRect(10, 50, 51, 22))
        self.get.setText(QtGui.QApplication.translate("MainWindow", "get", None, QtGui.QApplication.UnicodeUTF8))
        self.get.setObjectName(_fromUtf8("get"))
        self.url = QtGui.QLineEdit(self.centralwidget)
        self.url.setGeometry(QtCore.QRect(70, 20, 671, 27))
        self.url.setInputMethodHints(QtCore.Qt.ImhUrlCharactersOnly)
        self.url.setObjectName(_fromUtf8("url"))
        self.groupBox_2 = QtGui.QGroupBox(self.centralwidget)
        self.groupBox_2.setGeometry(QtCore.QRect(110, 50, 371, 111))
        self.groupBox_2.setTitle(QtGui.QApplication.translate("MainWindow", "data:", None, QtGui.QApplication.UnicodeUTF8))
        self.groupBox_2.setObjectName(_fromUtf8("groupBox_2"))
        self.dataname1 = QtGui.QLineEdit(self.groupBox_2)
        self.dataname1.setGeometry(QtCore.QRect(20, 30, 101, 27))
        self.dataname1.setObjectName(_fromUtf8("dataname1"))
        self.label = QtGui.QLabel(self.groupBox_2)
        self.label.setGeometry(QtCore.QRect(40, 10, 67, 17))
        self.label.setText(QtGui.QApplication.translate("MainWindow", "name:", None, QtGui.QApplication.UnicodeUTF8))
        self.label.setObjectName(_fromUtf8("label"))
        self.dataname2 = QtGui.QLineEdit(self.groupBox_2)
        self.dataname2.setGeometry(QtCore.QRect(130, 30, 113, 27))
        self.dataname2.setObjectName(_fromUtf8("dataname2"))
        self.dataname3 = QtGui.QLineEdit(self.groupBox_2)
        self.dataname3.setGeometry(QtCore.QRect(250, 30, 113, 27))
        self.dataname3.setObjectName(_fromUtf8("dataname3"))
        self.label_2 = QtGui.QLabel(self.groupBox_2)
        self.label_2.setGeometry(QtCore.QRect(40, 60, 67, 17))
        self.label_2.setText(QtGui.QApplication.translate("MainWindow", "value:", None, QtGui.QApplication.UnicodeUTF8))
        self.label_2.setObjectName(_fromUtf8("label_2"))
        self.datavalue1 = QtGui.QLineEdit(self.groupBox_2)
        self.datavalue1.setGeometry(QtCore.QRect(20, 80, 101, 27))
        self.datavalue1.setObjectName(_fromUtf8("datavalue1"))
        self.datavalue2 = QtGui.QLineEdit(self.groupBox_2)
        self.datavalue2.setGeometry(QtCore.QRect(130, 80, 113, 27))
        self.datavalue2.setObjectName(_fromUtf8("datavalue2"))
        self.datavalue3 = QtGui.QLineEdit(self.groupBox_2)
        self.datavalue3.setGeometry(QtCore.QRect(250, 80, 113, 27))
        self.datavalue3.setObjectName(_fromUtf8("datavalue3"))
        self.groupBox_4 = QtGui.QGroupBox(self.centralwidget)
        self.groupBox_4.setGeometry(QtCore.QRect(670, 50, 151, 111))
        self.groupBox_4.setTitle(QtGui.QApplication.translate("MainWindow", "password:", None, QtGui.QApplication.UnicodeUTF8))
        self.groupBox_4.setObjectName(_fromUtf8("groupBox_4"))
        self.passname = QtGui.QLineEdit(self.groupBox_4)
        self.passname.setGeometry(QtCore.QRect(10, 30, 113, 27))
        self.passname.setObjectName(_fromUtf8("passname"))
        self.passvalue = QtGui.QLineEdit(self.groupBox_4)
        self.passvalue.setGeometry(QtCore.QRect(10, 80, 113, 27))
        self.passvalue.setObjectName(_fromUtf8("passvalue"))
        self.passwordcheck = QtGui.QCheckBox(self.centralwidget)
        self.passwordcheck.setGeometry(QtCore.QRect(670, 180, 97, 22))
        self.passwordcheck.setText(QtGui.QApplication.translate("MainWindow", "password", None, QtGui.QApplication.UnicodeUTF8))
        self.passwordcheck.setChecked(True)
        self.passwordcheck.setObjectName(_fromUtf8("passwordcheck"))
        self.groupBox_5 = QtGui.QGroupBox(self.centralwidget)
        self.groupBox_5.setGeometry(QtCore.QRect(29, 169, 441, 81))
        self.groupBox_5.setTitle(QtGui.QApplication.translate("MainWindow", "proxy:", None, QtGui.QApplication.UnicodeUTF8))
        self.groupBox_5.setObjectName(_fromUtf8("groupBox_5"))
        self.proxyhost = QtGui.QLineEdit(self.groupBox_5)
        self.proxyhost.setGeometry(QtCore.QRect(20, 30, 113, 27))
        self.proxyhost.setObjectName(_fromUtf8("proxyhost"))
        self.proxyport = QtGui.QLineEdit(self.groupBox_5)
        self.proxyport.setGeometry(QtCore.QRect(140, 30, 51, 27))
        self.proxyport.setInputMethodHints(QtCore.Qt.ImhDigitsOnly|QtCore.Qt.ImhPreferNumbers)
        self.proxyport.setObjectName(_fromUtf8("proxyport"))
        self.proxyuser = QtGui.QLineEdit(self.groupBox_5)
        self.proxyuser.setGeometry(QtCore.QRect(200, 30, 113, 27))
        self.proxyuser.setObjectName(_fromUtf8("proxyuser"))
        self.proxypass = QtGui.QLineEdit(self.groupBox_5)
        self.proxypass.setGeometry(QtCore.QRect(320, 30, 113, 27))
        self.proxypass.setObjectName(_fromUtf8("proxypass"))
        self.label_4 = QtGui.QLabel(self.groupBox_5)
        self.label_4.setGeometry(QtCore.QRect(100, 10, 67, 17))
        self.label_4.setText(QtGui.QApplication.translate("MainWindow", "host", None, QtGui.QApplication.UnicodeUTF8))
        self.label_4.setObjectName(_fromUtf8("label_4"))
        self.label_5 = QtGui.QLabel(self.groupBox_5)
        self.label_5.setGeometry(QtCore.QRect(150, 10, 67, 17))
        self.label_5.setText(QtGui.QApplication.translate("MainWindow", "port", None, QtGui.QApplication.UnicodeUTF8))
        self.label_5.setObjectName(_fromUtf8("label_5"))
        self.label_6 = QtGui.QLabel(self.groupBox_5)
        self.label_6.setGeometry(QtCore.QRect(200, 10, 67, 17))
        self.label_6.setText(QtGui.QApplication.translate("MainWindow", "username", None, QtGui.QApplication.UnicodeUTF8))
        self.label_6.setObjectName(_fromUtf8("label_6"))
        self.label_7 = QtGui.QLabel(self.groupBox_5)
        self.label_7.setGeometry(QtCore.QRect(320, 10, 67, 17))
        self.label_7.setText(QtGui.QApplication.translate("MainWindow", "password", None, QtGui.QApplication.UnicodeUTF8))
        self.label_7.setObjectName(_fromUtf8("label_7"))
        self.groupBox_6 = QtGui.QGroupBox(self.centralwidget)
        self.groupBox_6.setGeometry(QtCore.QRect(30, 260, 531, 91))
        self.groupBox_6.setTitle(QtGui.QApplication.translate("MainWindow", "cookies:", None, QtGui.QApplication.UnicodeUTF8))
        self.groupBox_6.setObjectName(_fromUtf8("groupBox_6"))
        self.cookiename1 = QtGui.QLineEdit(self.groupBox_6)
        self.cookiename1.setGeometry(QtCore.QRect(10, 20, 113, 27))
        self.cookiename1.setObjectName(_fromUtf8("cookiename1"))
        self.cookiename2 = QtGui.QLineEdit(self.groupBox_6)
        self.cookiename2.setGeometry(QtCore.QRect(140, 20, 113, 27))
        self.cookiename2.setObjectName(_fromUtf8("cookename2"))
        self.cookiename3 = QtGui.QLineEdit(self.groupBox_6)
        self.cookiename3.setGeometry(QtCore.QRect(270, 20, 113, 27))
        self.cookiename3.setObjectName(_fromUtf8("cookiename3"))
        self.cookiename4 = QtGui.QLineEdit(self.groupBox_6)
        self.cookiename4.setGeometry(QtCore.QRect(390, 20, 113, 27))
        self.cookiename4.setObjectName(_fromUtf8("cookiename4"))
        self.cookievalue1 = QtGui.QLineEdit(self.groupBox_6)
        self.cookievalue1.setGeometry(QtCore.QRect(10, 50, 113, 27))
        self.cookievalue1.setObjectName(_fromUtf8("cookievalue1"))
        self.cookievalue2 = QtGui.QLineEdit(self.groupBox_6)
        self.cookievalue2.setGeometry(QtCore.QRect(140, 50, 113, 27))
        self.cookievalue2.setObjectName(_fromUtf8("cookievalue2"))
        self.cookievalue3 = QtGui.QLineEdit(self.groupBox_6)
        self.cookievalue3.setGeometry(QtCore.QRect(270, 50, 113, 27))
        self.cookievalue3.setObjectName(_fromUtf8("cookievalue3"))
        self.cookievalue4 = QtGui.QLineEdit(self.groupBox_6)
        self.cookievalue4.setGeometry(QtCore.QRect(390, 50, 113, 27))
        self.cookievalue4.setObjectName(_fromUtf8("cookievalue4"))
        self.groupBox_7 = QtGui.QGroupBox(self.centralwidget)
        self.groupBox_7.setGeometry(QtCore.QRect(570, 260, 251, 80))
        self.groupBox_7.setTitle(QtGui.QApplication.translate("MainWindow", "useragents:", None, QtGui.QApplication.UnicodeUTF8))
        self.groupBox_7.setObjectName(_fromUtf8("groupBox_7"))
        self.useragent = QtGui.QTextEdit(self.groupBox_7)
        self.useragent.setGeometry(QtCore.QRect(10, 20, 211, 51))
        self.useragent.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn)
        self.useragent.setObjectName(_fromUtf8("useragent"))
        self.start = QtGui.QPushButton(self.centralwidget)
        self.start.setGeometry(QtCore.QRect(750, 20, 71, 27))
        self.start.setText(QtGui.QApplication.translate("MainWindow", "start", None, QtGui.QApplication.UnicodeUTF8))
        self.start.setObjectName(_fromUtf8("start"))
        self.label_3 = QtGui.QLabel(self.centralwidget)
        self.label_3.setGeometry(QtCore.QRect(30, 20, 67, 17))
        self.label_3.setText(QtGui.QApplication.translate("MainWindow", "url :", None, QtGui.QApplication.UnicodeUTF8))
        self.label_3.setObjectName(_fromUtf8("label_3"))
        MainWindow.setCentralWidget(self.centralwidget)
        QtCore.QObject.connect(self.start, QtCore.SIGNAL(_fromUtf8("clicked(bool)")), myfun.main)
        QtCore.QObject.connect(self.passwordcheck, QtCore.SIGNAL(_fromUtf8("clicked(bool)")), self.groupBox_4.setEnabled)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)


def __del__():
    myfun.killer()
    os.abort()
    sys.exit()

app = QtGui.QApplication(sys.argv)
MainWindow = QtGui.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
myfun.log("\n\n--------------------------------------------------new session-------------------------------------\n\n")
MainWindow.show()
sys.exit(app.exec_())

1 个答案:

答案 0 :(得分:2)

我几乎不知道你的代码中的意图是什么,但这是导致你崩溃的有问题的代码行:

class flooderthread(QtCore.QThread):
    ...
    def run(self):
        connecter() # this is the culprit

fun.main()中,您只需拨打connecter()一次即可设置所有信号/插槽连接。此调用发生在主线程中。但是你有每个线程也调用该方法,它试图跨线程重复连接对象。实际的崩溃可能比这个函数更深,因为它是信号触发的结果,但我发现跳过代码以获得更深的东西太难了。很可能你正在让线程直接修改GUI元素。

一旦你修复了这个问题,你就会有另外一个错误导致它崩溃:

def main(self):
    connecter()
    ...
    time.sleep(3)
    gui()
    f5.start()
    gui()
    self.sleep(3)  # should be time.sleep(3)
    gui()
    ...

除此之外,我只想指出这个脚本非常难以理解。你到处都在使用全局变量,因此很难知道某些变量和函数调用的定义。你必须滚动一堆,追逐它们。您可能希望在继续滚雪球之前停止并重新组织它 我会高度建议拿起这样一本书:http://www.qtrac.eu/pyqtbook.html
你应该关注的是组织适当的课程。你当前的类结构需要大量的审查,所以你可能想要学习一本好的python书来首先学习如何编写类。我意识到你只是在学习,这就是为什么我希望这个建议能够帮助你指引正确的方向。