Results 1 to 3 of 3

Thread: QMapIterator

  1. #1
    Join Date
    Nov 2012
    Posts
    24
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default QMapIterator

    Hello,

    I have defined a pointer to a QMap<QString,QVector<int> > *mapSerie_, see the code below.
    Now I want to iterate over the map: QMapIterator<QString,QVector<int> > i = mapSerie_->begin();
    But this gives an error.
    How to define such a iterator?

    Regards,
    Arend


    The header:
    Qt Code:
    1. #include <QMainWindow>
    2. #include <QVector>
    3. #include <QMap>
    4. namespace Ui {
    5. class MainWindow;
    6. }
    7. class MainWindow : public QMainWindow
    8. {
    9. Q_OBJECT
    10. public:
    11. explicit MainWindow(QWidget *parent = 0);
    12. ~MainWindow();
    13. private:
    14. Ui::MainWindow *ui;
    15. private slots:
    16. QMap<QString,QVector<int> > *mapSerie();
    17. private:
    18. QMap<QString,QVector<int> > *mapSerie_;
    19. };
    To copy to clipboard, switch view to plain text mode 

    The source:
    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. #include "QMap"
    4. #include <QVector>
    5. #include <QMapIterator>
    6.  
    7. MainWindow::MainWindow(QWidget *parent) :
    8. QMainWindow(parent),
    9. ui(new Ui::MainWindow)
    10. {
    11. ui->setupUi(this);
    12. mapSerie_ = new QMap<QString,QVector<int> >;
    13.  
    14. QString name="One";
    15. QVector<int> serie;
    16.  
    17. for(size_t i=0;i<10;++i)
    18. serie.push_back(i);
    19.  
    20. mapSerie_->insert(name,serie);
    21. QMapIterator<QString,QVector<int> > i = mapSerie_->begin();
    22. }
    23.  
    24. MainWindow::~MainWindow()
    25. {
    26. delete ui;
    27. }
    28.  
    29. QMap<QString,QVector<int> > *MainWindow::mapSerie()
    30. {
    31. return mapSerie_;
    32. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QMapIterator

    QMap::begin() returns an STL (standard template library) style iterator, QMapIterator is a Java-style iterator and is created by passing the container to its constructor.

    So you can either to
    Qt Code:
    1. QMapIterator<QString, QVector<int> > i(*mapSerie_);
    To copy to clipboard, switch view to plain text mode 
    or
    Qt Code:
    1. QMap<QString, QVector<int> >::const_iterator i = mapSerie->begin();
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

  3. #3
    Join Date
    Nov 2012
    Posts
    24
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QMapIterator

    Thanks, the first one works.
    Arend

Similar Threads

  1. QT newbie: QMapIterator not showing any output, but foreach does?
    By Robert Kulagowski in forum Qt Programming
    Replies: 6
    Last Post: 1st June 2012, 15:33
  2. bad behaviour of QMapIterator
    By zorro68 in forum Qt Programming
    Replies: 2
    Last Post: 14th February 2008, 22:16

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.