//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"
}
}
}
QT 交互的那些事
最新推荐文章于 2023-07-14 17:28:38 发布