Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. Adding items to a list
Forum Updated to NodeBB v4.3 + New Features

Adding items to a list

Scheduled Pinned Locked Moved C++ Gurus
35 Posts 5 Posters 19.7k 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.
  • M Offline
    M Offline
    Monkey666
    wrote on 1 Aug 2011, 10:31 last edited by
    #1

    Hi guys, I am an advanced C# coder who is trying to transition over to real coding in C++ but getting stuck on some hurdles, which normally would be easy in C#.

    Here is what I am trying to do:

    @
    MyForm::MyForm(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::MyForm)
    {
    ui->setupUi(this);
    ui->myList->clear();
    if (createProcessList())
    {
    list<ProcInfo>::const_iterator p;
    for (p=procList.begin();p!=procList.end(); ++p)
    {
    ui->myList->addItems(*p);
    }
    }
    }
    @

    In C# this would work by using the base string To.String() to name the items when adding to a list, however here it seems alot different and I cannot figure it out.

    Any help would be appreciated. Thanks.

    Edit: Forgot to mention, procList is a list of processes already generated elsewhere in the application:

    @
    list<ProcInfo> procList;
    @

    1 Reply Last reply
    0
    • L Offline
      L Offline
      loladiro
      wrote on 1 Aug 2011, 10:38 last edited by
      #2

      Well if there's no implicit conversion from your ProcInfo class to QString, you'd have to call the appropriate method yourself e.g.
      @
      ui->myList->addItem((*p).name());
      @
      Also, in your snippet you use addItems, while (I guess), it should be addItem.

      1 Reply Last reply
      0
      • M Offline
        M Offline
        Monkey666
        wrote on 1 Aug 2011, 10:43 last edited by
        #3

        Hi, I seem to get a compile error:

        error: 'const class ProcInfo' has no member named 'name'.

        I'm guessing I have to create the method, in which case how would one create a QString method inside the ProcInfo class?

        1 Reply Last reply
        0
        • D Offline
          D Offline
          dangelog
          wrote on 1 Aug 2011, 10:48 last edited by
          #4

          Of course! Adapt that snippet to what you're actually doing!

          What's your ui->myList? And what's a ProcInfo?

          Software Engineer
          KDAB (UK) Ltd., a KDAB Group company

          1 Reply Last reply
          0
          • M Offline
            M Offline
            Monkey666
            wrote on 1 Aug 2011, 10:51 last edited by
            #5

            myList is a listbox widget on the app, and ProcInfo:

            @class ProcInfo
            {
            public:
            HANDLE Proc;
            DWORD Base;
            int ID;
            };@

            1 Reply Last reply
            0
            • D Offline
              D Offline
              dangelog
              wrote on 1 Aug 2011, 10:59 last edited by
              #6

              And exactly what do you want to add to that list? Because there's nothing resembling a string inside your ProcInfo class...

              Software Engineer
              KDAB (UK) Ltd., a KDAB Group company

              1 Reply Last reply
              0
              • M Offline
                M Offline
                Monkey666
                wrote on 1 Aug 2011, 11:01 last edited by
                #7

                I want to add each ProcInfo contained in the list to the listbox, but from your repsonse I am guessing you can't add custom class items to a listbox the same way you can in C#.

                1 Reply Last reply
                0
                • L Offline
                  L Offline
                  loladiro
                  wrote on 1 Aug 2011, 11:09 last edited by
                  #8

                  even in C# you need to have a ToString method. How would C++ what you actually want to print. That being said. What do you want to print? The id? In that case you could have an implicit cast to QString like:
                  @
                  class ProcInfo
                  {
                  public:
                  //code
                  operator QString() const
                  {
                  return QString::number(id);
                  }
                  };
                  @
                  or you could call QString::number in the loop instead of adding an implicit cast function:
                  @
                  ui->myList->addItem(QString::number((*p).id));
                  @

                  1 Reply Last reply
                  0
                  • M Offline
                    M Offline
                    Monkey666
                    wrote on 1 Aug 2011, 11:23 last edited by
                    #9

                    [quote author="loladiro" date="1312196973"]even in C# you need to have a ToString method. How would C++ what you actually want to print. [/quote]

                    No you don't in C#, because when you create a class it automatically has a base ToString() method which is automatically used in certain controls like listboxes without being called (although the string would be simply the class name unless you override the method) I assumed C++ would be no different but clearly it is.

                    Thanks for the help.

                    1 Reply Last reply
                    0
                    • M Offline
                      M Offline
                      Monkey666
                      wrote on 1 Aug 2011, 11:35 last edited by
                      #10

                      How would I use your operator QString() const method? I can tell I have a long way to go int he conversion from C# to C++ :(

                      1 Reply Last reply
                      0
                      • L Offline
                        L Offline
                        loladiro
                        wrote on 1 Aug 2011, 11:36 last edited by
                        #11

                        Sorry, I forgot about that in C# (I have only once looked into it and that very briefly). However, if you consider your issue solved, pleas add [Solved] in front of the thread title (by editing the first post).

                        1 Reply Last reply
                        0
                        • M Offline
                          M Offline
                          Monkey666
                          wrote on 1 Aug 2011, 11:37 last edited by
                          #12

                          I can't work out actually how to use your operator QString() const.

                          1 Reply Last reply
                          0
                          • L Offline
                            L Offline
                            loladiro
                            wrote on 1 Aug 2011, 11:39 last edited by
                            #13

                            The posts must have crossed in the air ;). The QString() const method is an "explicit coversion operator":http://msdn.microsoft.com/en-us/library/ts48df3y(v=vs.80).aspx . However, be aware that there are also many bugs that may follow from the misuse of this laguage feature.

                            EDIT: you can use it just like ou would in C# (i.e. without doing anything special)

                            1 Reply Last reply
                            0
                            • M Offline
                              M Offline
                              Monkey666
                              wrote on 1 Aug 2011, 11:46 last edited by
                              #14

                              That's what I thought but get compiler error:

                              error: no matching function for call to 'QListWidget::addItem(const ProcInfo&)'

                              1 Reply Last reply
                              0
                              • M Offline
                                M Offline
                                Monkey666
                                wrote on 1 Aug 2011, 11:51 last edited by
                                #15

                                Actually there are more compile errors I should have mentioned which are most likely causing this:

                                error: no match for 'operator=' in 'p = procList.std::list<_Tp, _Alloc>::begin with _Tp = int, _Alloc = std::allocator<int>'

                                1 Reply Last reply
                                0
                                • L Offline
                                  L Offline
                                  loladiro
                                  wrote on 1 Aug 2011, 12:02 last edited by
                                  #16

                                  Sorry but I can't reproduce. This all works:
                                  @
                                  class ProcInfo
                                  {
                                  public:
                                  int id;
                                  ProcInfo() : id(1234)
                                  {}
                                  operator QString() const
                                  {
                                  return QString::number(id);
                                  }
                                  };

                                  void print(QString print)
                                  {
                                  qDebug() << print;
                                  }

                                  int main(int argc, char *argv[])
                                  {
                                  QApplication a(argc, argv);

                                  QList< ProcInfo > list1;
                                  list1 << ProcInfo();
                                  QList< ProcInfo >::const_iterator i = list1.begin();
                                  
                                  std::list< ProcInfo > list2;
                                  list2.push_back(ProcInfo());
                                  
                                  std::list< ProcInfo >::const_iterator i2 = list2.begin();
                                  
                                  print(*i2);
                                  
                                  print (*(i));
                                  
                                  QListWidget widget;
                                  widget.addItem(*i);
                                  
                                  std::list< ProcInfo >::const_iterator p = list2.begin();
                                  
                                  for (p=list2.begin();p!=list2.end(); ++p)
                                  {
                                      widget.addItem(*p);
                                  }
                                  
                                  widget.show();
                                  
                                  const ProcInfo p2;
                                  print(p2);
                                  
                                  return a.exec(&#41;;
                                  

                                  }
                                  @

                                  1 Reply Last reply
                                  0
                                  • M Offline
                                    M Offline
                                    Monkey666
                                    wrote on 1 Aug 2011, 12:19 last edited by
                                    #17

                                    Ok I've managed to get rid of all compile errors except this one:

                                    error: expected type-specifier before 'QString'

                                    And the error points to the operator:

                                    @
                                    class ProcInfo
                                    {
                                    public:
                                    HANDLE Proc;
                                    DWORD Base;
                                    int ID;
                                    operator QString() const
                                    {
                                    return QString::number(ID);
                                    }
                                    };@

                                    1 Reply Last reply
                                    0
                                    • L Offline
                                      L Offline
                                      loladiro
                                      wrote on 1 Aug 2011, 12:24 last edited by
                                      #18

                                      did you #include <QString>?

                                      1 Reply Last reply
                                      0
                                      • M Offline
                                        M Offline
                                        Monkey666
                                        wrote on 1 Aug 2011, 12:32 last edited by
                                        #19

                                        That fixed that but now I have another problem /sigh.

                                        I have declared std::list<ProcInfo> procList; inside a header file, and that header file is included in the main cpp file which I am doing this all in, however I get error:

                                        error: multiple definition of `procList'

                                        There is definitely only 1 definition and it's inside the h file so I'm a little confused, maybe c++ isn't for me :(

                                        1 Reply Last reply
                                        0
                                        • L Offline
                                          L Offline
                                          loladiro
                                          wrote on 1 Aug 2011, 12:37 last edited by
                                          #20

                                          There's not much we can do without seeing the header file ...

                                          1 Reply Last reply
                                          0

                                          1/35

                                          1 Aug 2011, 10:31

                                          • Login

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