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. Error with QList
Qt 6.11 is out! See what's new in the release blog

Error with QList

Scheduled Pinned Locked Moved General and Desktop
37 Posts 5 Posters 30.2k Views 1 Watching
  • 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.
  • G Offline
    G Offline
    gogoi
    wrote on last edited by
    #1

    Hello friends,I am trying to execute the code given in Qtdocs..

    @#include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include <QtCore/QCoreApplication>
    #include <QFileInfo>
    #include <QList>
    #include <qlist.h>
    MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent), ui(new Ui::MainWindow)
    {
    ui->setupUi(this);
    readSettings();
    }

    MainWindow::~MainWindow()
    {
    saveSettings();
    delete ui;
    }

    void MainWindow::readSettings()
    {

    struct Login {
         QString userName;
         QString password;
     };
    

    template < typename T>;
    QList<Login> logins;

     QSettings settings;
     int size = settings.beginReadArray("logins");
     for (int i = 0; i < size; ++i) {
         settings.setArrayIndex(i);
         Login login;
         login.userName = settings.value("userName").toString();
         login.password = settings.value("password").toString();
         logins.append(login);
     }
     settings.endArray();
    

    }

    void MainWindow::saveSettings()
    {
    struct Login {
    QString userName;
    QString password;
    };
    template<typename T>;
    QList <Login> logins;

     QSettings settings;
     settings.beginWriteArray("logins");
     for (int i = 0; i < logins.size(); ++i) {
         settings.setArrayIndex(i);
         settings.setValue("userName", list.at(i).userName);
         settings.setValue("password", list.at(i).password);
     }
     settings.endArray();
    }@
    

    But I am getting the below errors.

    bq. error: template argument for 'template<class T> class QList' uses local type 'MainWindow::readSettings()::Login'
    error: trying to instantiate 'template<class T> class QList'
    error: invalid type in declaration before ';' token
    error: request for member 'append' in 'logins', which is of non-class type 'int'

    Can anybody help me out ?

    Regards
    gogoi

    1 Reply Last reply
    0
    • C Offline
      C Offline
      cincirin
      wrote on last edited by
      #2

      Try to put your Login struct at the beginning of your source code, or in mainwindow.h
      Also, is not necessary to instantiate logins object with template<typename T>. If you just write @QList<Login>@ should be ok.

      1 Reply Last reply
      0
      • G Offline
        G Offline
        gogoi
        wrote on last edited by
        #3

        hey thanks cincirin,for your reply,I declared the struct in class in mainwindow.h and made it public.No I am getting on error as list is not declared..So i declared it below way
        QList<QString>list;

        But Now again I get a error
        bq. error: 'const class QString' has no member named 'userName'
        error: 'const class QString' has no member named 'password'

        regards
        gogoi

        1 Reply Last reply
        0
        • C Offline
          C Offline
          cincirin
          wrote on last edited by
          #4

          The line @QList<QString>list@ is ok. The problem is elsewhere in your code. Can you show the entire code ? Also why you include QList header twice ?

          1 Reply Last reply
          0
          • G Offline
            G Offline
            gogoi
            wrote on last edited by
            #5

            Hello cincirin

            entire code i have pasted above only.I included headers twicw by mistake,sorry for that..

            regards
            gogoi

            1 Reply Last reply
            0
            • C Offline
              C Offline
              cincirin
              wrote on last edited by
              #6

              Ok. I wrote a simple source code as follow:
              @
              struct Login {
              QString userName;
              QString password;
              };
              #include <QSettings>

              void readSettings()
              {

               QList<Login> logins;
              
               QSettings settings;
               int size = settings.beginReadArray("logins");
               for (int i = 0; i < size; ++i) {
                   settings.setArrayIndex(i);
                   Login login;
                   login.userName = settings.value("userName").toString();
                   login.password = settings.value("password").toString();
                   logins.append(login);
               }
               settings.endArray();
              

              }
              @

              This code work for me.

              Also make sure you have included QList and QString headers.

              1 Reply Last reply
              0
              • G Offline
                G Offline
                gogoi
                wrote on last edited by
                #7

                hey cincirin..I have also written the samething and the error is in savesettings function..
                below is my Mainwindow.h

                @#ifndef MAINWINDOW_H
                #define MAINWINDOW_H

                #include <QtGui/QMainWindow>
                #include <QSettings>
                #include <QList>
                #include <qlist.h>

                namespace Ui {
                class MainWindow;
                }

                class MainWindow : public QMainWindow
                {
                Q_OBJECT
                public:
                struct Login {
                QString userName;
                QString password;
                };

                enum ScreenOrientation {
                    ScreenOrientationLockPortrait,
                    ScreenOrientationLockLandscape,
                    ScreenOrientationAuto
                };
                
                explicit MainWindow(QWidget *parent = 0);
                virtual ~MainWindow();
                
                // Note that this will only have an effect on Symbian and Fremantle.
                void setOrientation(ScreenOrientation orientation);
                
                void showExpanded();
                

                private slots:
                void on_set_clicked();

                private:
                Ui::MainWindow *ui;
                void readSettings();
                void saveSettings();
                };

                #endif // MAINWINDOW_H@

                regards
                gogoi

                1 Reply Last reply
                0
                • G Offline
                  G Offline
                  gogoi
                  wrote on last edited by
                  #8

                  hello cincirin
                  in the below function..
                  I am getting error

                  @void MainWindow::saveSettings()
                  {

                  QList <Login> logins;
                  QList<QString> list;
                   QSettings settings;
                   settings.beginWriteArray("logins");
                   for (int i = 0; i < logins.size(); ++i) {
                       settings.setArrayIndex(i);
                       settings.setValue("userName", list.at(i).userName);//error
                       settings.setValue("password", list.at(i).password);//error
                   }
                   settings.endArray();
                  

                  }@

                  regards
                  gogoi

                  1 Reply Last reply
                  0
                  • K Offline
                    K Offline
                    koahnig
                    wrote on last edited by
                    #9

                    Hi gogoi

                    you might have to be a little more specific on your error (message).

                    Vote the answer(s) that helped you to solve your issue(s)

                    1 Reply Last reply
                    0
                    • G Offline
                      G Offline
                      gogoi
                      wrote on last edited by
                      #10

                      thanks all error is solved..

                      regards
                      gogoi

                      1 Reply Last reply
                      0
                      • V Offline
                        V Offline
                        vinb
                        wrote on last edited by
                        #11

                        Do you mind to enlight us?
                        How did you solve your problem?

                        1 Reply Last reply
                        0
                        • C Offline
                          C Offline
                          cincirin
                          wrote on last edited by
                          #12

                          Her/His list variable was QList<QString> type and not QList<Login> type.

                          1 Reply Last reply
                          0
                          • G Offline
                            G Offline
                            gogoi
                            wrote on last edited by
                            #13

                            hey friends,can anybody help me out in how shall i append the username and passwd data into the list..

                            regards
                            gogoi

                            1 Reply Last reply
                            0
                            • C Offline
                              C Offline
                              cincirin
                              wrote on last edited by
                              #14

                              "see append function":http://doc.qt.nokia.com/latest/qlist.html#append
                              In your code ... :
                              @
                              QList<Login> loginsList;
                              Login login = {"userName", "password"};
                              loginsList.append(login);
                              @

                              1 Reply Last reply
                              0
                              • G Offline
                                G Offline
                                gogoi
                                wrote on last edited by
                                #15

                                ok thanks cincirin,I will read it and get back to u.

                                regards
                                gogoi

                                1 Reply Last reply
                                0
                                • G Offline
                                  G Offline
                                  gogoi
                                  wrote on last edited by
                                  #16

                                  hey cincirin,
                                  suppose i want to take username and password from userinput..then how shall i do it..
                                  I mean..

                                  QList<Login> loginsList;
                                  Login login = {ui->lineEdit->text(), ui->lineEdit_2->text()};
                                  loginsList.append(login);

                                  regards
                                  gogoi

                                  1 Reply Last reply
                                  0
                                  • C Offline
                                    C Offline
                                    cincirin
                                    wrote on last edited by
                                    #17

                                    Yes, it's ok.
                                    Or, if you want to add more Logins in your list, create only one instance of Login
                                    @
                                    Login login;

                                    login.userName = ui->lineEdit->text();
                                    login.password = ui->lineEdit_2->text();
                                    loginsList.append(login);

                                    login.userName = ui->lineEdit->text();
                                    login.password = ui->lineEdit_2->text();
                                    loginsList.append(login);
                                    @

                                    1 Reply Last reply
                                    0
                                    • G Offline
                                      G Offline
                                      gogoi
                                      wrote on last edited by
                                      #18

                                      @void MainWindow::readSettings()
                                      {

                                      QList<Login> logins;
                                      
                                         
                                       QSettings settings;
                                       int size = settings.beginReadArray("logins");
                                       for (int i = 0; i < size; ++i) {
                                           settings.setArrayIndex(i);
                                           Login login;
                                           login.userName = settings.value(("userName").toString(),ui->lineEdit->setText());
                                           login.password = settings.value(("password").toString(),ui->lineEdit_2->settext());
                                           
                                           logins.append(login);
                                       }
                                       settings.endArray();
                                      

                                      }@

                                      can i do it this way cincirin..

                                      regards
                                      gogoi

                                      1 Reply Last reply
                                      0
                                      • C Offline
                                        C Offline
                                        cincirin
                                        wrote on last edited by
                                        #19

                                        No, you can't.
                                        You need to separate @QSettings::value@ from @QlineEdit::setText@
                                        @
                                        void MainWindow::readSettings()
                                        {
                                        QList<Login> logins;
                                        QSettings settings;
                                        int size = settings.beginReadArray("logins");
                                        for (int i = 0; i < size; ++i) {
                                        settings.setArrayIndex(i);
                                        Login login;
                                        login.userName = settings.value(("userName").toString();
                                        ui->lineEdit->setText(login.userName));
                                        login.password = settings.value(("password").toString();
                                        ui->lineEdit_2->settext(login.password));

                                             logins.append(login);
                                         }
                                         settings.endArray();
                                        

                                        }
                                        @

                                        1 Reply Last reply
                                        0
                                        • G Offline
                                          G Offline
                                          gogoi
                                          wrote on last edited by
                                          #20

                                          thanks cincirin...then how shall i write in writesettings function..because when i debug..the login size is showing zero only

                                          regards
                                          gogoi

                                          1 Reply Last reply
                                          0

                                          • Login

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