有时我们展示了一个列表, 并想提供查看某项列表的详细内容, 我们会在列表项的末端加一个查看按钮, 这时我们如何在按按钮的时候得知这是那一项呢 这时就需要带参数的信号, 信号是可以带参数的, 参数会在信号发送时携带, 并传递给接收此信号的槽
from PyQt4 import QtGui, QtCore
class MyButton(QtGuiQPushButton):
myclicked = QtCorepyqtSignal(int)
def __init__(self, _id, args, kwargs):
QtGuiQPushButton__init__(self, args, kwargs)
self_id = _id
selfconnect(self, QtCoreSIGNAL("clicked()"), selfemitMyclicked)
def emitMyclicked(self):
selfmyclickedemit(self_id)
app = QtGuiQApplication([])
w = QtGuiQWidget()
wresize(100, 100)
def showMsg(_id):
QtGuiQMessageBoxinformation(w, u"信息", u"查看 %d" % _id)
btn = MyButton(1, u"查看1", w)
wconnect(btn, QtCoreSIGNAL("myclicked(int)"), showMsg)
btn2 = MyButton(2, u"查看2", w)
btn2move(0, 30)
wconnect(btn2, QtCoreSIGNAL("myclicked(int)"), showMsg)
wshow()
appexec_()
上面例子可以看出, QObjectemit 发送带参数的信号时要携带参数 当然上面例子也可以用下面方式来写
from PyQt4 import QtGui, QtCore
class MyButton(QtGuiQPushButton):
def __init__(self, _id, args, kwargs):
self_id = _id
QtGuiQPushButton__init__(self, args, kwargs)
selfconnect(self, QtCoreSIGNAL("clicked()"), selfemitClicked)
def emitClicked(self):
selfemit(QtCoreSIGNAL("myclicked(int)"), self_id)
app = QtGuiQApplication([])
w = QtGuiQWidget()
wresize(100, 100)
def showMsg(_id):
QtGuiQMessageBoxinformation(w, u"信息", u"查看 %d" % _id)
btn = MyButton(1, u"查看1", w)
wconnect(btn, QtCoreSIGNAL("myclicked(int)"), showMsg)
btn2 = MyButton(2, u"查看2", w)
btn2move(0, 30)
wconnect(btn2, QtCoreSIGNAL("myclicked(int)"), showMsg)
wshow()
appexec_()
lz是在编译qt相关的代码是吧?
出现这种问题,一般是你缺少了部分库(lib)的链接过程。请检查下工程的设置。看看有哪些依赖项目需要手动添加。
因为lz没有提供足够多的细节(使用什么IDE,用什么编译器,qt版本如何,什么样的工程等等)。只能给出上面的大概解决方案。详细请lz按照上面所说的方向自己再研究。
QMetaObject::connectSlotsByName: No matching signal for on_pushButton_clicked()
他这句话的意思是,你之前UI创建了一个pushButton的按钮,而且选择了关联槽,之后你将该pussButton给删除了或者改了名字,此时系统找不到改pushButton进行信号关联 *** 作,因此会提示你这个错误,你类里面关于该函数的函数删除就行了,记得是类定义里面的。
使用方法1 继承QT thread 重写run
void DataUnpacket::run( )
{
m_pSocket = new QTcpSocket;
//m_pSocket->socketOption(QAbstractSocket::LowDelayOption);
QString ip("192168112100");
ushort portData = 5001;
m_pSocket->connectToHost( ip, portData );
m_pSocket->waitForConnected();
if( QAbstractSocket::ConnectedState != m_pSocket->state() )//判断是否连接成功
return;
while( m_bRunning )
{
if( m_pSocket->bytesAvailable() > 0)
{
QByteArray arr = m_pSocket->readAll(); //这不进,
qDebug() << "recv len =" << arrlength() ;
}
else
{
m_pSocket->write("test",4);
m_pSocket->flush();// 可以发送出去
QThread::usleep(1000);
}
}
}
方法二 movethred 效果 一样
方法三 SocketQThread中使用 QtConcurrent::run创建线程, 这个使用QTCreater 调试没有问题,这个也是大坑
打包会出现不能接收到数据或者没发出去, wireshark检测有时会出现现象接收到数据但没发出去
int SocketQThread::InitTest( )
{
int re = 0;
QString ip("1921681125");
ushort portData = 5001;
m_pDataPackageresize(1);
m_pDataPackage[0] = new DataUnpacket;
if( !m_pDataPackage[0]->InitSocket(ip,portData ) )
re = -1;
return re;
}
void SocketQThread::stratThred()
{
m_running = true;
for(int i=0;i<1;i++)//测试
{
QtConcurrent::run(this, &SocketQThread::recvDataThread, i );
}
}
void SocketQThread::stopThred()
{
m_running = false;
for(int i=0;i<1;i++)//测试
{
m_pDataPackage[0]->CloseFile( );
m_pDataPackage[0]->tcpDisconnect( );
}
}
void SocketQThread::recvDataThread( int ch )//暂定
{
DataUnpacket pPackage = m_pDataPackage[ch];
while( m_running )
{
if( pPackage->SocketBytesAvailable() > 0)
pPackage->RecvData();
else
QThread::usleep(1);
}
}
类DataUnpacket 继承 Cpackage 如下:
m_Socket为Cpackage的成员变量
int DataUnpacket::RecvData( )
{
int re = 0;
QByteArray reArr;
QByteArray arr = m_SocketreadAll();
qDebug() << "recv len =" << arrlength() ;
#if 0
m_Socketwrite(arr );
m_Socketflush();//不加发不出去
#else
QMetaObject::invokeMethod( &m_Socket, std::bind( static_cast< qint64(QTcpSocket::)(const QByteArray &) >( &QTcpSocket::write ),&m_Socket, arr ));//, Qt::DirectConnection
//若加 Qt::DirectConnection ,需要添加m_Socketflush();/
return re;
}
bool Cpackage::InitSocket(QString& ip, ushort& port )
{
bool tf = true;
m_SocketconnectToHost( ip, port );
m_SocketwaitForConnected();
if( QAbstractSocket::ConnectedState != m_Socketstate() )//判断是否连接成功
tf = false;
return tf;
}
void Cpackage::tcpDisconnect( )
{
if( m_Socketstate() == QAbstractSocket::ConnectedState) //关闭时,确保与服务器断开连接
m_SocketdisconnectFromHost();
}
方法四 QtConcurrent::run(this, &DataUnpacket::RecvDataTest); 这个
void testData::RecvDataTest()
{
m_pSocket = new QTcpSocket(this);
//m_pSocket->socketOption(QAbstractSocket::LowDelayOption);
QString ip("192168112100");
ushort portData = 5001;
m_pSocket->connectToHost( ip, portData );
m_pSocket->waitForConnected();
if( QAbstractSocket::ConnectedState != m_pSocket->state() )//判断是否连接成功
return;
while( m_bRunning )
{
if( m_pSocket->bytesAvailable() > 0)
{
QByteArray arr = m_pSocket->readAll();//这不进,
qDebug() << "recv len =" << arrlength() ;
}
else
{
m_pSocket->write("test",4);
m_pSocket->flush();//可以发送出去
QThread::usleep(1000);
}
}
}
如下例子 是可以的
void testData::RecvDataTest()
{
qDebug() << QThread::currentThread();
QByteArray arr = m_pSocket->readAll();//这不进,
qDebug() << "recv len =" << arrlength() ;
m_pSocket->write("test",4);
}
void testData::stratThred( bool tf )
{
if( tf )
start();
else
exit();
}
void testData::run( )
{
qDebug() << "stratThred" << QThread::currentThread();
if( nullptr == m_pSocket )
{
m_pSocket = new QTcpSocket;
connect(m_pSocket, SIGNAL(readyRead()), this, SLOT(RecvDataTest()), Qt::DirectConnection);
//m_pSocket->socketOption(QAbstractSocket::LowDelayOption);
}
m_pSocket->connectToHost( QString("192168112100"), 5001 );
m_pSocket->waitForConnected();
if( QAbstractSocket::ConnectedState != m_pSocket->state() )//判断是否连接成功
return;
//m_pSocket->waitForReadyRead(1000);
exec();
m_pSocket->disconnectFromHost();
}
>
BIAS0:= (C-MA(C,2))/MA(C,2)100;
BIAS1 := (C-MA(C,12))/MA(C,12)100;
BIAS2 := (C-MA(C,26))/MA(C,26)100;
BIAS3 := (C-MA(C,48))/MA(C,48)100;
HXL:=V/CAPITAL100;
D1:=INDEXC;
D2:=MA(D1,56);
DR2:=D1/D2<094;
E1:=(C-HHV(C,12))/HHV(C,12)10;
E2:=(C-REF(C,26))/REF(C,26)10;
以上就是关于使用pyqt4写GUI小程序时一个clicked信号调用三次槽函数全部的内容,包括:使用pyqt4写GUI小程序时一个clicked信号调用三次槽函数、无法解析的外部符号 C++、Qt Ui中多建立了 一个槽 怎么删除干净等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)