Error with QList



  • 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



  • 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.



  • 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



  • 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 ?



  • Hello cincirin

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

    regards
    gogoi



  • 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.



  • 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



  • 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


  • Moderators

    Hi gogoi

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



  • thanks all error is solved..

    regards
    gogoi



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



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



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

    regards
    gogoi



  • "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);
    @



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

    regards
    gogoi



  • 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



  • 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);
    @



  • @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



  • 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();
    

    }
    @



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

    regards
    gogoi



  • In a similar manner as read settings.
    But I think your problem is that you want to use the same list both in read & write settings. In this case, your problem C++ and not Qt. So, if I'm right, you need to declare QList<Login> member of your class, and not separate instances in read&write functions.



  • ya i have not created two instance of QList<login>..
    please check out this

    @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);
         Login login;  
        ui->lineEdit->settext(login.userName);
    
         settings.setValue("password", list.at(i).password);
          ui->lineEdit_2->settext(login.password);
     }
     settings.endArray();
    

    }
    @

    regards
    gogoi



  • In your 10 and 13 lines, both line edits will have blank text. Use list.at(i) instead of local login variable.
    If your list is member of MainWindow class, or if it's global in MainWindow.cpp, your code should be ok, if you remove line 9.



  • hey thanks for the info..i did that,,but no effect ,my .ini file shows only this output..

    [logins]
    size=0

    regards
    gogoi



  • If you have @QList<Login> m_logins@ member in your MainWindow class, and if in read function you'll use m_logins.append, I'm sure that write function will write in ini file as many Login as you appended before.



  • No cincirin..I coded the 2 functions according to your suggestions..but its not entering the loop in the savesettings function..and everytime it says size=0;//
    so i think on click of a button we shouls set the username and passwd..
    this way..
    @void MainWindow::on_set_clicked()
    {

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

    }@

    i dont know whether i m right or wrong..u please just check out//

    regards
    gogoi



  • Ok, in the following code if you call Test::count function, you'll see as many Login as you appended:
    @
    class Test
    {
    public:
    void append();
    int count()const;

    protected:
    struct Login
    {
    QString userName;
    QString password;
    };

    QList<Login> m_loginsList;
    

    };

    void Test::append()
    {
    Login log = {"user", "pwd"};
    m_loginsList.append(log);
    }

    int Test::count()const
    {
    return m_loginsList.size();
    }

    int main(int argc, char *argv[])
    {
    Test t;
    t.append(); // one item
    t.append(); // two items
    // ...
    qDebug("%d", t.count()); // print how many items are in list
    return 0;
    }
    @



  • ok.thanks..so whether my code is correct..?

    hey now my ini file is showing..the data which i input through the lineedit..but the username and password are not appending in the list..I mean the next time when i run and enter usrname and passwd..the previous username and password will be lost..

    regards
    gogoi



  • In the next time when you run the application, first read settings from ini, and append in logins list



  • from where shall i call it?
    readsettiing(); or writesettings(); and how?

    regards
    gogoi



  • these are my working functions..now please help me out..how shall i first read settings from ini and append in logins list..

    @void MainWindow::readSettings()
    {

     QSettings settings;
     int size = settings.beginReadArray("logins");
    
    
     for (int i = 0; i <size; ++i)
     {
         settings.setArrayIndex(i);
    
         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();
    

    }

    void MainWindow::saveSettings()
    {
    QSettings settings;

     login.userName=ui->lineEdit->text();
     login.password=ui->lineEdit_2->text();
    
     logins.append(login);
    
     settings.beginWriteArray("logins");
    
     for (int i = 0; i < logins.size(); ++i) {
         settings.setArrayIndex(i);
         settings.setValue("userName", logins.at(i).userName);
         settings.setValue("password", logins.at(i).password);
    
     }
     settings.endArray();
    

    }
    @

    regards
    gogoi



  • hey i got it..



  • Hi cicncirin r u there,
    Hey Whether we can get the date in an .ini file?

    regards
    gogoi



  • Yes, sure. You can write an "variant":http://doc.qt.nokia.com/latest/qvariant.html#QVariant-23
    Then, you can read variant from ini and transform to "date time":http://doc.qt.nokia.com/latest/qvariant.html#toDateTime



  • ok thanks cincirin...hey one morething i like to ask u..Actually i want to open a virtual keyboard on click of a pushbutton..so how shall i do it...

    regards
    gogoi





  • [quote author="gogoi" date="1306838572"]ok thanks cincirin...hey one morething i like to ask u..Actually i want to open a virtual keyboard on click of a pushbutton..so how shall i do it...
    regards
    gogoi[/quote]

    You've opened a "separate thread":http://developer.qt.nokia.com/forums/viewthread/6371/ for this, so I'm closing this one.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.