Error with QList
Page |
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>
- {
- ui->setupUi(this);
- readSettings();
- }
- MainWindow::~MainWindow()
- {
- saveSettings();
- delete ui;
- }
- void MainWindow::readSettings()
- {
- struct Login {
- QString userName;
- QString password;
- };
- template < typename T>;
- 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>;
- 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.
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
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
Ok. I wrote a simple source code as follow:
- struct Login {
- QString userName;
- QString password;
- };
- #include <QSettings>
- void readSettings()
- {
- 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.
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;
- }
- {
- Q_OBJECT
- public:
- struct Login {
- QString userName;
- QString password;
- };
- enum ScreenOrientation {
- ScreenOrientationLockPortrait,
- ScreenOrientationLockLandscape,
- ScreenOrientationAuto
- };
- 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
hello cincirin
in the below function..
I am getting error
- void MainWindow::saveSettings()
- {
- 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
see append function [doc.qt.nokia.com]
In your code … :
- Login login = {"userName", "password"};
- loginsList.append(login);
You must log in to post a reply. Not a member yet? Register here!



