How to take input from user Graphically ?



  • i have this function ( i have only put a snippet of it here , it's too big and is not relevant to question here )

    void dbmanager::add()
    {
    
        QString text ;
        int pageid , revid;
    
    
    
        // create custom temporary event loop on stack
        QEventLoop eventLoop;
    
        // "quit()" the event-loop, when the network request "finished()"
        QNetworkAccessManager mgr;
        QObject::connect(&mgr, SIGNAL(finished(QNetworkReply*)), &eventLoop, SLOT(quit()));
    
        // the HTTP request
        QNetworkRequest req( QUrl( QString("http://en.wikitolearn.org/api.php?action=parse&page=Linear_Algebra/Relations&format=json") ) );
        QNetworkReply *reply = mgr.get(req);
        eventLoop.exec();
    

    in the QUrl is have a provided a static string there . What i want is that user provides me input to construct the url .

    Let me make it bit clear here . The above function is in a class and not in my main.cpp file .

    This function is called from a QML file , when a user clicks a button this function ( add) is called

    In short what i want to do is : When user clicks the button , i want something that takes user input , so that i can construct URL's .

    I hope i was clear in explaining what i want .

    P.S. English is not my first language ,



  • Hi! So your question is "how to make a URL input dialog in QtQuick"?



  • Yes , kind of . I actually don't care whether user provide me whole url , what i would prefer is title from user .

    I want to do it from C++ ( if possible ) .

    Thanks for the help !!



  • Sry, I still don't get. ^_^ First you said:

    This function is called from a QML file

    Later you said:

    I want to do it from C++

    Is your GUI written in QtQuick or not? Or do you use some QWidgets / QtQuick hybrid?



  • Sorry for making it confusing :/ .

    i will try to explain properly

    imports used in my QML file

    import QtQuick 2.3
    import QtQuick.Window 2.2
    import QtQuick.Controls 1.4
    import QtQuick.Dialogs 1.2
    import QtWebEngine 1.1
    import QtWebKit 3.0
    import QtQuick 2.5
    
    

    my qml file snippet :

    Button{
            id : save
            width: root.width
            height: root.height /6
            text: "SAVE PAGE"
            onClicked: {
                    msg.visible = true
                    dbm.add()
    
                    
            }
        }
    

    my cpp file

    void dbmanager::add()
    {
    
        QString text ;
        int pageid , revid;
    
    
    
        // create custom temporary event loop on stack
        QEventLoop eventLoop;
    
        // "quit()" the event-loop, when the network request "finished()"
        QNetworkAccessManager mgr;
        QObject::connect(&mgr, SIGNAL(finished(QNetworkReply*)), &eventLoop, SLOT(quit()));
    
        // the HTTP request
        QNetworkRequest req( QUrl( QString("http://en.wikitolearn.org/api.php?action=parse&page=Linear_Algebra/Relations&format=json") ) );
        QNetworkReply *reply = mgr.get(req);
        eventLoop.exec();
    

    As you can see , when user clicks on that button , this ( add ) function from a cpp file is called .

    So yes , the function is called from QML button .

    What i meant when i said " want to do it from C++ "

    what i meant was that , that the user input box or input box should be called from that add() function only .

    Why i said that " i want to do it from C++ "

    because to be honest i don't know how to pass QML data/values from QML to C++ :( .

    If you want to see whole codebase , i can give it :)



  • Hi! Look at the following code. It has a class named Backend which acts as the interface of your C++ business logic to your QtQuick GUI. This interface inherits QObject:

    backend.h

    #ifndef BACKEND_H
    #define BACKEND_H
    
    #include <QObject>
    
    class Backend : public QObject
    {
        Q_OBJECT
    public:
        explicit Backend(QObject *parent = 0);
    
        Q_INVOKABLE QString add(QString someUrl);
    };
    
    #endif // BACKEND_H
    

    backend.cpp

    #include "backend.h"
    
    Backend::Backend(QObject *parent) :
        QObject(parent)
    {
    }
    
    QString Backend::add(QString someUrl)
    {
        // do something
        static auto i = 0;
        return QString("%1: %2").arg(++i).arg(someUrl);
    }
    

    In the main function the Backend class is made available to the QML type system with qmlRegisterType. We instantiate a single backend object and insert it into the QML context with setContextProperty:

    main.cpp

    #include <QGuiApplication>
    #include <QQmlApplicationEngine>
    #include <QtQml>
    
    #include "backend.h"
    
    
    int main(int argc, char *argv[])
    {
        QGuiApplication app(argc, argv);
        qmlRegisterType<Backend>("io.qt.forum", 1, 0, "Backend");
        Backend backend;
        QQmlApplicationEngine engine;
        engine.rootContext()->setContextProperty( "backend", &backend );
        engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
        return app.exec();
    }
    

    And finally our main.qml with a custom Dialog that calls a method of our backend object:

    main.qml

    import QtQuick 2.5
    import QtQuick.Controls 1.4
    import QtQuick.Dialogs 1.2
    
    import io.qt.forum 1.0
    
    ApplicationWindow {
        visible: true
        width: 600
        height: 400
        color: "plum"
    
        Dialog {
            id: myDialog
            visible: false
            title: "Title of Dialog"
            contentItem: Rectangle {
                color: "orange"
                implicitWidth: 400
                implicitHeight: 100
                Column {
                    Text {
                        text: "Enter some string!"
                    }
                    TextField {
                        id: myTextField
                        width: 300
                        text: ""
                    }
                    Row {
                        Button {
                            text: "Ok"
                            onClicked: {
                                responseText.text = backend.add(myTextField.text)
                                myDialog.close()
                            }
                        }
                        Button {
                            text: "Cancel"
                            onClicked: myDialog.close()
                        }
                    }
                }
            }
        }
    
        Row {
            spacing: 20
            Button {
                text: "Add"
                onClicked: myDialog.open();
            }
            Text {
                id: responseText
            }
        }
    
    }
    

    Hope it helps!



  • Hey @Wieland . Thanks for the help . It is working fine . ( i will customize it now to my needs )


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.