QT 交互的那些事

//widget.h
 #ifndef WIDGET_H 
 #define WIDGET_H 
 #include <QWidget> 
 #include <QGraphicsProxyWidget> 
 #include <QPushButton> 
 #include <QLabel> 
 #include <QLineEdit> 
 class Widget : public QGraphicsProxyWidget 
 {     Q_OBJECT public:     explicit Widget(QGraphicsItem *parent = 0);
      ~Widget();     
      Q_INVOKABLE void changeText(const QString& s); 
      signals:     void sendOnButton(void);
       private:     QPushButton *m_Btn;     
       QLabel      *m_Label;     
       QWidget     *m_MainWidget; 
}; 
#endif // WIDGET_H


//widget.cpp 
#include "widget.h" 
Widget::Widget(QGraphicsItem *parent) :     
QGraphicsProxyWidget(parent) {     
	m_MainWidget = new QWidget;    
	 m_Btn = new QPushButton(m_MainWidget);     
	 m_Label = new QLabel(m_MainWidget);     
	 m_Btn->setText("PushButton");     
	 m_Btn->setGeometry(10, 10, 100, 30);     
	 m_Label->setGeometry(10, 40, 200, 30);     
	 QObject::connect(m_Btn, SIGNAL(clicked()), this, SIGNAL(sendOnButton()));     
	 setWidget(m_MainWidget); 
} 
Widget::~Widget() {
     delete m_MainWidget; 
} 
void Widget::changeText(const QString& s) {    
		  m_Label->setText(s);    
		   qDebug(" call Widget::changeText");
}


// main.cpp 
#include <QtGui/QApplication> 
#include <QtDeclarative/QDeclarativeView> 
#include <QtDeclarative/QDeclarativeEngine> 
#include <QtDeclarative/QDeclarativeComponent> 
#include <QtDeclarative/QDeclarativeContext> 
#include "widget.h" 
int main(int argc, char *argv[]) {
	     QApplication a(argc, argv);     
	     qmlRegisterType<Widget>("UIWidget", 1, 0, "Widget");     
	     QDeclarativeView qmlView;     
	     qmlView.setSource(QUrl::fromLocalFile("../UICtest/UICtest.qml"));     
	     qmlView.show();     
	     return a.exec();
}


// UICtest.qml 
import Qt 4.7 
import UIWidget 1.0 
Rectangle{     
	width: 640     
	height: 480     
	color: "black"     
	Widget{ id: uiwidget; x: 100; y: 100; width: 400; height: 100;    
	     // 关键在这里,当一个信号导出后他的相应的名字就是第1个字母大写,前面在加上on      
	        // 例如 clicked -- onClicked   colorchange --onColorchange;     
	       onSendOnButton: { 
					uiwidget.changeText(textinput.text); 
					}     
	}
	Rectangle{         x: 100; y: 20; width: 400; height: 30; 
	      color: "blue"         
	      TextInput{
			  id: textinput;
			  anchors.fill: parent; 
			  color: "white" 
		 } 
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值