Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Getting QLineedit values to qt header files

Getting QLineedit values to qt header files

Scheduled Pinned Locked Moved Unsolved General and Desktop
qtcreatorheader cpp qtline editinput
7 Posts 3 Posters 2.0k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Lasith
    wrote on 12 Sept 2017, 17:37 last edited by Lasith 9 Dec 2017, 17:41
    #1

    I want to create a database connection method named connectionOpen in my header file inorder to connect to a database when user enters user name, password, hostname and port number! I can create the method by assigning values manually but when I try to give from the UI inputs it is impossible!
    The followin code works!

    public: QSqlDatabase mydb;
    bool connectionOpen(){
    mydb.setDatabaseName("XE");
    mydb.setHostName(123.145.123.456);
    mydb.setUserName("uname");
    mydb.setPassword("21213");
    mydb.setPort(1521);
    }

    But the following code returns errors!
    public: QSqlDatabase mydb;
    bool connectionOpen(){
    mydb.setDatabaseName(ui->lineEdit->setText());
    mydb.setHostName(ui->lineEdit2->setText());
    mydb.setUserName(ui->lineEdit3->setText());
    mydb.setPassword(ui->lineEdit4->setText());
    mydb.setPort(ui->lineEdit5->setText());
    }

    How can I correct this?

    1 Reply Last reply
    0
    • V Offline
      V Offline
      Vinod Kuntoji
      wrote on 12 Sept 2017, 17:52 last edited by
      #2

      @Lasith ,

      QLineEdit is having returnpressed() signal,

      connect(ui->lineEdit,SIGNAL(returnPressed()),this,SLOT(catchString()));

      void Widget::catchString() {
      QString name = ui->lineEdit->text();
      mydb.setDatabaseName(name);
      }

      C++, Qt, Qt Quick Developer,
      PthinkS, Bangalore

      L 1 Reply Last reply 12 Sept 2017, 18:12
      1
      • V Vinod Kuntoji
        12 Sept 2017, 17:52

        @Lasith ,

        QLineEdit is having returnpressed() signal,

        connect(ui->lineEdit,SIGNAL(returnPressed()),this,SLOT(catchString()));

        void Widget::catchString() {
        QString name = ui->lineEdit->text();
        mydb.setDatabaseName(name);
        }

        L Offline
        L Offline
        Lasith
        wrote on 12 Sept 2017, 18:12 last edited by
        #3

        @Vinod-Kuntoji Thanx alot! for the 5 string inputs do we need to have connect methods?

        1 Reply Last reply
        1
        • V Offline
          V Offline
          Vinod Kuntoji
          wrote on 12 Sept 2017, 18:14 last edited by
          #4

          @Lasith ,
          Yes.

          C++, Qt, Qt Quick Developer,
          PthinkS, Bangalore

          L 1 Reply Last reply 12 Sept 2017, 18:16
          1
          • V Vinod Kuntoji
            12 Sept 2017, 18:14

            @Lasith ,
            Yes.

            L Offline
            L Offline
            Lasith
            wrote on 12 Sept 2017, 18:16 last edited by
            #5

            @Vinod-Kuntoji Do I have to place the code you put in the header file or the cpp?

            V 1 Reply Last reply 12 Sept 2017, 18:20
            0
            • L Lasith
              12 Sept 2017, 18:16

              @Vinod-Kuntoji Do I have to place the code you put in the header file or the cpp?

              V Offline
              V Offline
              Vinod Kuntoji
              wrote on 12 Sept 2017, 18:20 last edited by
              #6

              @Lasith,

              In cpp file

              C++, Qt, Qt Quick Developer,
              PthinkS, Bangalore

              1 Reply Last reply
              0
              • S Offline
                S Offline
                SGaist
                Lifetime Qt Champion
                wrote on 12 Sept 2017, 20:37 last edited by
                #7

                Hi,

                Your code won't work, your QSqlDatabase object is invalid.

                Please take the time to look at the code example in QSqlDatabase's documentation as well as the example provided.

                It' also discouraged to store database object like that locally. QSqlDatabase allows for easy connection handling without the need for these local copies.

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                1

                5/7

                12 Sept 2017, 18:16

                • Login

                • Login or register to search.
                5 out of 7
                • First post
                  5/7
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved