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
QtWS25 Last Chance

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 last edited by Lasith
    #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
    • Vinod KuntojiV Offline
      Vinod KuntojiV Offline
      Vinod Kuntoji
      wrote on 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
      1
      • Vinod KuntojiV Vinod Kuntoji

        @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 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
        • Vinod KuntojiV Offline
          Vinod KuntojiV Offline
          Vinod Kuntoji
          wrote on last edited by
          #4

          @Lasith ,
          Yes.

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

          L 1 Reply Last reply
          1
          • Vinod KuntojiV Vinod Kuntoji

            @Lasith ,
            Yes.

            L Offline
            L Offline
            Lasith
            wrote on last edited by
            #5

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

            Vinod KuntojiV 1 Reply Last reply
            0
            • L Lasith

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

              Vinod KuntojiV Offline
              Vinod KuntojiV Offline
              Vinod Kuntoji
              wrote on last edited by
              #6

              @Lasith,

              In cpp file

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

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on 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

                • Login

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