TerabaytusJune 1, 2018, 5:20 a.m.
Как из TextField получить текст в слое С++?
Добрый день, подскажите пожалуйста. В слое QML есть TextField как получить из него данные в С++ при использование данного кода приложение сразу закрывается и пишет в лог
Программа неожиданно завершилась.
Процесс был завершён принудительно.
import QtQuick 2.9 import QtQuick.Window 2.2 import QtQuick 2.6 import QtQuick.Controls 1.4 import QtQuick.Controls 2.1 Window { id: window visible: true width: 300 height: 310 maximumHeight: height maximumWidth: width minimumHeight: height minimumWidth: width title: qsTr("") Connections { target: backend onSendToQml: { console.log("QML " + count); } } TextField { id: textField objectName: "sp" x: 9 y: 36 } Switch { id: control x: 0 y: 0 width: 300 height: 50 text: qsTr("") checked: false onClicked: { backend.setBackend(control); } contentItem: Text { rightPadding: control.indicator.width + control.spacing text: control.text font: control.font opacity: enabled ? 1.0 : 0.3 color: control.down ? "#17a81a" : "#21be2b" elide: Text.ElideRight verticalAlignment: Text.AlignVCenter } indicator: Rectangle { implicitWidth: 48 implicitHeight: 26 x: control.width - width - control.rightPadding y: parent.height / 2 - height / 2 radius: 13 color: control.checked ? "#17a81a" : "transparent" border.color: control.checked ? "#17a81a" : "#cccccc" Rectangle { x: control.checked ? parent.width - width : 0 width: 26 height: 26 radius: 13 color: control.down ? "#cccccc" : "#ffffff" border.color: control.checked ? (control.down ? "#17a81a" : "#21be2b") : "#999999" } } background: Rectangle { implicitWidth: 100 implicitHeight: 40 visible: control.down /*|| control.highlighted*/ color: control.down ? "#bdbebf" : "#eeeeee" } }
#include <QGuiApplication> #include <QQmlApplicationEngine> #include <QQmlContext> #include "backend.h" int main(int argc, char *argv[]) { QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); QGuiApplication app(argc, argv); QQmlApplicationEngine engine; BackEnd backend; QQmlContext *context = engine.rootContext(); context->setContextProperty("backend", &backend); engine.load(QUrl(QStringLiteral("qrc:/main.qml"))); return -1; return app.exec(); }
#ifndef BACKEND_H #define BACKEND_H #include <QObject> #include <QString> class BackEnd : public QObject { Q_OBJECT public: explicit BackEnd(QObject *parent = 0); signals: void sendToQml(int count); private slots: void setBackend(bool control); public: }; #endif // BACKEND_H
#include "backend.h" #include <iostream> BackEnd::BackEnd(QObject *parent) : QObject(parent) { } void BackEnd::setBackend(bool control) { //Найдем строки ввода QObject* field1 = this->parent()->findChild<QObject*>("sp"); //Считаем информацию со строк ввода через свойство text QString str1=(field1->property("text")).toString(); // std::cout << str1 << std::endl ; }
We recommend hosting TIMEWEB
Stable hosting, on which the social network EVILEG is located. For projects on Django we recommend VDS hosting.Do you like it? Share on social networks!
B
- Bogdannn
- March 27, 2024, 7:15 p.m.
C++ - Test 001. The first program and data types
- Result:46points,
- Rating points-6
Last comments
Qt Linux - Lesson 001. Autorun Qt application under Linux как сделать автозапуск для флэтпака, который не даёт создавать файлы в ~/.config - вот это вопрос ))
АК
Qt WinAPI - Lesson 007. Working with ICMP Ping in Qt Без строки #include <QRegularExpressionValidator> в заголовочном файле не работает валидатор.
Анатолий КононенкоFeb. 5, 2024, 1:50 a.m.
EVADec. 25, 2023, 10:30 a.m.
Boost - static linking in CMake project under Windows Сделал всё по-как у вас, но выдаёт ошибку [build] LINK : fatal error LNK1104: не удается открыть файл "libboost_locale-vc142-mt-gd-x64-1_74.lib" Хоть убей, не могу понять в чём дел…
Qt/C++ - Lesson 056. Connecting the Boost library in Qt for MinGW and MSVC compilers Для решения твой проблемы добавь в файл .pro строчку "LIBS += -lws2_32" она решит проблему , лично мне помогло.
Now discuss on the forum
Как получить в массив значения из связанной модели? Спасибо, разобрался:))
AC
QML Обнулить значения SpinBox Доброго времени суток, не могу разобраться с обнулением значение SpinBox находящего в делегате. import QtQuickimport QtQuick.ControlsWindow { width: 640 height: 480 visible: tr…
Alexandru CodreanuJan. 19, 2024, 11:57 a.m.
BlinCTDec. 27, 2023, 8:57 a.m.
ДмитрийJan. 10, 2024, 4:18 a.m.
Evgenii LegotckoiDec. 12, 2023, 6:48 a.m.
уточню что в функции
есть условиеДобрый день!
Согласен с вами, но видимо не всё так просто сейчас Qt ругается на
а если его за комментировать то пишет