QML widgets in QWidget
I've a QWidget and I've some Simples Controlls like Button, ProgressBar, TextEdit etc.. done in QML which I am taking from QML Examples. I was reading http://doc.qt.io/archives/qt-4.7/qtbinding.html and http://doc.qt.io/archives/qt-4.7/qml-integration.html at the moment I am trying QDeclarativeView.
setAttribute(Qt::WA_TranslucentBackground);
layout = new QVBoxLayout(this);
QDeclarativeView* btnView = new QDeclarativeView;
btnView->setSource(QUrl::fromLocalFile("Button.qml"));
QObject* btnObj = btnView->rootObject();
btnObj->setProperty("width", 140);
btnObj->setProperty("height", 32);
btnObj->setProperty("text", "Close");
//progressBar = new QProgressBar(this);
button = new QPushButton("Click", this);
//layout->addWidget(progressBar);
layou开发者_JAVA技巧t->addWidget(button);
btnView->setGeometry(0, 0, 140, 32);
btnView->setBaseSize(140, 32);
layout->addWidget(btnView);
setLayout(layout);
Why my btnView is taking this lot of Space ? and also is it (QDeclarativeView) a Good way of doing ? or there exists an even better solution ?
When using QDeclarativeComponent I get an QObject. But not a QWidget . and is this even a Good solution ? Whats the Best Solution to have lucrative Simple Widgets in Traditional QWidget ?
You didnt tell the layout how to size the widgets. So it will divide the space equally between all its children, in this case since it has only 1 child , the QDeclarativeView takes up the entire space. Use QLayout::setSizePolicy() and QWidget::setMaximumWidth() / QWidget::setMaximumHeight() to resize according to your wishes. You can also use QLayout::addSpacing();
QDeclarativeView is a decent way of adding new UI elements to QWidgets. But it is slower than using QWidgets themselves.
精彩评论