Qt 信号-槽函数(signal - slot)

发布于:2025-09-11 ⋅ 阅读:(12) ⋅ 点赞:(0)

新建一个form1窗体后,ui中拖入控件lineEdit,radioButton、radioButton_2、QGroupBox,
radioButton、radioButton_2放在QGroupBox上面,因为此时会在radioButton之间有一个聚焦,

form1.h

public slots:
	void ontext1Changed( QWidget* pSource);
	void onedit1GotFocus( QWidget* pSource);
	void onkeyUp1( int nKey, int nButtonState, bool& rbIgnoreEvent, QWidget* pSource);
	
	void onradioButton1Sel( QWidget* pSource);
	void onradioButton2Sel( QWidget* pSource);
	void onkeyUpRadio1( int nKey, int nButtonState, bool& rbIgnoreEvent, QWidget* pSource);
	void onkeyUpRadio2( int nKey, int nButtonState, bool& rbIgnoreEvent, QWidget* pSource);

form1.cpp

form1::form1(QWidget* pParent, const QString& rszName)
	: SlGfwDialogForm(pParent, rszName)
{
	setupUi(this);

	QObject::connect(this->lineEdit,
					SIGNAL(valueChanged( QWidget *)),
					this,
					SLOT( ontext1Changed( QWidget *)));
					
	QObject::connect(this->lineEdit,
					SIGNAL(gotFocus( QWidget *)),
					this,
					SLOT( onedit1GotFocus( QWidget *)));
	
	QObject::connect(this->lineEdit,
					SIGNAL( keyUp( int, int, bool&, QWidget*)),
					this,
					SLOT( onkeyUp1( int, int, bool&, QWidget*)));

	QObject::connect(this->radioButton,
					SIGNAL(gotFocus( QWidget *)),
					this,
					SLOT( onradioButton1Sel( QWidget *)));
					
	QObject::connect(this->radioButton_2,
					SIGNAL(gotFocus( QWidget *)),
					this,
					SLOT( onradioButton2Sel( QWidget *)));
					
	QObject::connect(this->radioButton,
					SIGNAL( keyUp( int, int, bool&, QWidget*)),
					this,
					SLOT( onkeyUpRadio1( int, int, bool&, QWidget*)));
					
	QObject::connect(this->radioButton_2,
					SIGNAL( keyUp( int, int, bool&, QWidget*)),
					this,
					SLOT( onkeyUpRadio2( int, int, bool&, QWidget*)));
}// <-- form1::form1 ()
void form1::ontext1Changed( QWidget* pSource)
{
	//this->lineEdit->setValue(QVariant(text));
	QVariant text = this->lineEdit->value();
	//...=text.toString();  //自行处理数据
}

void form1::onedit1GotFocus(QWidget *pSource)
{
	this->lineEdit->setEditMode(true);
	this->lineEdit->end(true);
}

void form1::onkeyUp1( int nKey, int nButtonState, bool& rbIgnoreEvent, QWidget* pSource)
{
	QVariant vdata;
	bool bRet = this->lineEdit->currentValue(vdata);
	if ( bRet == true)
	{
		QString text = vdata.toString();
		//... = text;  //自行处理数据
	}
}
void form1::onradioButton1Sel( QWidget* pSource)
{
	this->radioButton->setChecked(true);
	this->radioButton_2->setChecked(false);
}

void form1::onradioButton2Sel( QWidget* pSource)
{
	this->radioButton->setChecked(false);
	this->radioButton_2->setChecked(true);
}

void form1::onkeyUpRadio1( int nKey, int nButtonState, bool& rbIgnoreEvent, QWidget* pSource)
{
	QMessageBox msgBox;
	//msgBox.setText("onKeyUpRadio: Start");
	//msgBox.exec();
	bool btab = false;
	if ( nKey == Qt::Key_Up)
	{
		//rbIgnoreEvent = true;
		//msgBox.setText("onKeyUpRadio: Key_Up");
		//msgBox.exec();
		btab = true;
	}
	else if ( nKey == Qt::Key_Down)
	{
		//rbIgnoreEvent = true;
		//msgBox.setText("onKeyUpRadio: Key_Down");
		//msgBox.exec();
		btab = true;
	}
	else if ( nKey == Qt::Key_Tab)
	{
		//rbIgnoreEvent = true;
		//msgBox.setText("onKeyUpRadio: Key_Down");
		//msgBox.exec();
		btab = true;
	}
	if ( btab == true)
	{
		if ( !this->radioButton->isChecked())
		{
			rbIgnoreEvent = true;
			this->radioButton_2->setFocus();
		}
	}
	else
	{
		this->onradioButton1Sel( pSource);
	}
}

void form1::onkeyUpRadio2( int nKey, int nButtonState, bool& rbIgnoreEvent, QWidget* pSource)
{
	QMessageBox msgBox;
	//msgBox.setText("onKeyUpRadio: Start");
	//msgBox.exec();
	bool btab = false;
	if ( nKey == Qt::Key_Up)
	{
		//rbIgnoreEvent = true;
		//msgBox.setText("onKeyUpRadio: Key_Up");
		//msgBox.exec();
		btab = true;
	}
	else if ( nKey == Qt::Key_Down)
	{
		//rbIgnoreEvent = true;
		//msgBox.setText("onKeyUpRadio: Key_Down");
		//msgBox.exec();
		btab = true;
	}
	else if ( nKey == Qt::Key_Tab)
	{
		//rbIgnoreEvent = true;
		//msgBox.setText("onKeyUpRadio: Key_Down");
		//msgBox.exec();
		btab = true;
	}
	if ( btab == true)
	{
		if ( !this->radioButton_2->isChecked())
		{
			rbIgnoreEvent = true;
			this->radioButton->setFocus();
		}
	}
	else
	{
		this->onradioButton2Sel( pSource);
	}
}

网站公告

今日签到

点亮在社区的每一天
去签到