May 27, 2011

gogoi gogoi
Lab Rat
58 posts

Error with QList

Page  
1

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

  1. #include "mainwindow.h"
  2. #include "ui_mainwindow.h"
  3. #include <QtCore/QCoreApplication>
  4. #include <QFileInfo>
  5. #include <QList>
  6. #include <qlist.h>
  7. MainWindow::MainWindow(QWidget *parent)
  8.     : QMainWindow(parent), ui(new Ui::MainWindow)
  9. {
  10.     ui->setupUi(this);
  11.     readSettings();
  12. }
  13.  
  14. MainWindow::~MainWindow()
  15. {
  16.     saveSettings();
  17.     delete ui;
  18. }
  19.  
  20. void MainWindow::readSettings()
  21. {
  22.  
  23.     struct Login {
  24.          QString userName;
  25.          QString password;
  26.      };
  27.    template < typename T>;
  28.     QList<Login> logins;
  29.  
  30.      QSettings settings;
  31.      int size = settings.beginReadArray("logins");
  32.      for (int i = 0; i < size; ++i) {
  33.          settings.setArrayIndex(i);
  34.          Login login;
  35.          login.userName = settings.value("userName").toString();
  36.          login.password = settings.value("password").toString();
  37.          logins.append(login);
  38.      }
  39.      settings.endArray();
  40.   }
  41.  
  42. void MainWindow::saveSettings()
  43. {
  44.     struct Login {
  45.          QString userName;
  46.          QString password;
  47.      };
  48.     template<typename T>;
  49.     QList <Login> logins;
  50.  
  51.  
  52.      QSettings settings;
  53.      settings.beginWriteArray("logins");
  54.      for (int i = 0; i < logins.size(); ++i) {
  55.          settings.setArrayIndex(i);
  56.          settings.setValue("userName", list.at(i).userName);
  57.          settings.setValue("password", list.at(i).password);
  58.      }
  59.      settings.endArray();
  60.     }

But I am getting the below errors.

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

36 replies

May 27, 2011

cincirin cincirin
Ant Farmer
388 posts

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

  1. QList<Login>
should be ok.

May 27, 2011

gogoi gogoi
Lab Rat
58 posts

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

May 27, 2011

cincirin cincirin
Ant Farmer
388 posts

The line

is ok. The problem is elsewhere in your code. Can you show the entire code ? Also why you include QList header twice ?

May 27, 2011

gogoi gogoi
Lab Rat
58 posts

Hello cincirin

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

regards
gogoi

May 27, 2011

cincirin cincirin
Ant Farmer
388 posts

Ok. I wrote a simple source code as follow:

  1. struct Login {
  2.      QString userName;
  3.      QString password;
  4.  };
  5. #include <QSettings>
  6.  
  7. void readSettings()
  8. {
  9.  
  10.      QList<Login> logins;
  11.  
  12.      QSettings settings;
  13.      int size = settings.beginReadArray("logins");
  14.      for (int i = 0; i < size; ++i) {
  15.          settings.setArrayIndex(i);
  16.          Login login;
  17.          login.userName = settings.value("userName").toString();
  18.          login.password = settings.value("password").toString();
  19.          logins.append(login);
  20.      }
  21.      settings.endArray();
  22.   }

This code work for me.

Also make sure you have included QList and QString headers.

May 27, 2011

gogoi gogoi
Lab Rat
58 posts

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

  1. #ifndef MAINWINDOW_H
  2. #define MAINWINDOW_H
  3.  
  4. #include <QtGui/QMainWindow>
  5. #include <QSettings>
  6. #include <QList>
  7. #include <qlist.h>
  8.  
  9. namespace Ui {
  10.     class MainWindow;
  11. }
  12.  
  13. class MainWindow : public QMainWindow
  14. {
  15.     Q_OBJECT
  16. public:
  17.     struct Login {
  18.          QString userName;
  19.          QString password;
  20.      };
  21.  
  22.  
  23.  
  24.  
  25.     enum ScreenOrientation {
  26.         ScreenOrientationLockPortrait,
  27.         ScreenOrientationLockLandscape,
  28.         ScreenOrientationAuto
  29.     };
  30.  
  31.     explicit MainWindow(QWidget *parent = 0);
  32.     virtual ~MainWindow();
  33.  
  34.     // Note that this will only have an effect on Symbian and Fremantle.
  35.     void setOrientation(ScreenOrientation orientation);
  36.  
  37.     void showExpanded();
  38.  
  39. private slots:
  40.     void on_set_clicked();
  41.  
  42. private:
  43.     Ui::MainWindow *ui;
  44.   void readSettings();
  45.     void saveSettings();
  46.     };
  47.  
  48. #endif // MAINWINDOW_H

regards
gogoi

May 27, 2011

gogoi gogoi
Lab Rat
58 posts

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

  1. void MainWindow::saveSettings()
  2. {
  3.  
  4.    
  5.     QList <Login> logins;
  6.     QList<QString> list;
  7.      QSettings settings;
  8.      settings.beginWriteArray("logins");
  9.      for (int i = 0; i < logins.size(); ++i) {
  10.          settings.setArrayIndex(i);
  11.          settings.setValue("userName", list.at(i).userName);//error
  12.          settings.setValue("password", list.at(i).password);//error
  13.      }
  14.      settings.endArray();
  15.  
  16. }

regards
gogoi

May 27, 2011

koahnig koahnig
Mad Scientist
2199 posts

Hi gogoi

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

May 27, 2011

gogoi gogoi
Lab Rat
58 posts

thanks all error is solved..

regards
gogoi

May 27, 2011

vinb vinb
Lab Rat
205 posts

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

May 27, 2011

cincirin cincirin
Ant Farmer
388 posts

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

May 30, 2011

gogoi gogoi
Lab Rat
58 posts

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

regards
gogoi

May 30, 2011

cincirin cincirin
Ant Farmer
388 posts

see append function [doc.qt.nokia.com]
In your code … :

  1.     QList<Login> loginsList;
  2.     Login login = {"userName", "password"};
  3.     loginsList.append(login);

May 30, 2011

gogoi gogoi
Lab Rat
58 posts

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

regards
gogoi

May 30, 2011

gogoi gogoi
Lab Rat
58 posts

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

Page  
1

  ‹‹ What to use for monetary precision.      QMatrix vs QTransform ››

You must log in to post a reply. Not a member yet? Register here!