Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Arrays And an error when trying to declare them
Forum Updated to NodeBB v4.3 + New Features

Arrays And an error when trying to declare them

Scheduled Pinned Locked Moved General and Desktop
16 Posts 5 Posters 10.5k 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.
  • R Offline
    R Offline
    RichardM198030
    wrote on 24 Sept 2011, 22:42 last edited by
    #1

    I'm having issues with Arrays in Qt, I'm including what I typed, and got an error about no known conversions from int to QString:

    [code]
    int NFC_North[4] = {"Baltimore Ravens", "Cincinnati Bengal", "Cleveland Browns", "Pittsburgh Steelers"};
    [/code]

    I even tried using char, and QString, both threw an error, the other error for QString was throwing and error about not being able to find some file, char threw and error about not being able to use my array, too many items.

    Any other way to do this? =/. The loop part I can do myself, it's this part I'm stuck with, =/. Thanks !!

    1 Reply Last reply
    0
    • V Offline
      V Offline
      vsorokin
      wrote on 24 Sept 2011, 23:05 last edited by
      #2

      You define array of int and try fill it string values, obviously, you get compiler error.

      Minimal working example for you:

      @#include <QString>
      #include <QtDebug>

      int main(int argc, char *argv[])
      {
      QString NFC_North[4] = {"Baltimore Ravens", "Cincinnati Bengal", "Cleveland Browns", "Pittsburgh Steelers"};

      qDebug() << NFC_North[0] << NFC_North[1] << NFC_North[2] << NFC_North[3];
      return 0;
      

      }@

      --
      Vasiliy

      1 Reply Last reply
      0
      • R Offline
        R Offline
        RichardM198030
        wrote on 24 Sept 2011, 23:59 last edited by
        #3

        [quote author="Vass" date="1316905540"]You define array of int and try fill it string values, obviously, you get compiler error.

        Minimal working example for you:

        @#include <QString>
        #include <QtDebug>

        int main(int argc, char *argv[])
        {
        QString NFC_North[4] = {"Baltimore Ravens", "Cincinnati Bengal", "Cleveland Browns", "Pittsburgh Steelers"};

        qDebug() << NFC_North[0] << NFC_North[1] << NFC_North[2] << NFC_North[3];
        return 0;
        

        }@

        [/quote]

        Thanks, but these are going to be put into a QComBox;

        @
        QComboBox *NFC_West_ComboBox;
        NFC_West_ComboBox = new QComboBox(Team_Layout);
        @
        // I haven't tried putting in there yet, so I have defined that wrong. There's going to be a total of 8 ComboBoxes, seeing there's divisions with 4 Teams in each, figured using layouts would make the UI look nicer, =).

        1 Reply Last reply
        0
        • A Offline
          A Offline
          andre
          wrote on 25 Sept 2011, 10:21 last edited by
          #4

          Instead of using arrays, look into using the standard Qt container classes. They are much easier to use. Containers in Qt include QList, QVector, QHash, QMap and some others. In your case, you might want to look into using QStringList.

          1 Reply Last reply
          0
          • R Offline
            R Offline
            RichardM198030
            wrote on 25 Sept 2011, 19:51 last edited by
            #5

            [quote author="Andre" date="1316946119"]Instead of using arrays, look into using the standard Qt container classes. They are much easier to use. Containers in Qt include QList, QVector, QHash, QMap and some others. In your case, you might want to look into using QStringList.[/quote]

            Heh awesome thanks !! Only reason why I'm saying heh, is because I can get Gentoo intalled, and KDE running, but yet, I can't do something so simple as getting an Array working, =P.. LOL

            1 Reply Last reply
            0
            • G Offline
              G Offline
              goetz
              wrote on 26 Sept 2011, 06:35 last edited by
              #6

              You most probably want to use [[Doc:QStringList]] in order to populate the combobox with "QComboBox::addItems() ":http://doc.qt.nokia.com/4.7/qcombobox.html#addItems

              http://www.catb.org/~esr/faqs/smart-questions.html

              1 Reply Last reply
              0
              • R Offline
                R Offline
                RichardM198030
                wrote on 27 Sept 2011, 06:38 last edited by
                #7

                [quote author="Volker" date="1317018923"]You most probably want to use [[Doc:QStringList]] in order to populate the combobox with "QComboBox::addItems() ":http://doc.qt.nokia.com/4.7/qcombobox.html#addItems[/quote]

                THanks for the post, starting to get irritated here, I did it how I thought it was going to be, I have included my ScoreBoard.h ScoreBoard.cpp Scoreboard.pro and main.cpp files, I cannot seem to figure out what I'm doing wrong.. Here's an error Im getting:

                [code]
                /home/rottingdead/Desktop/Programming_Folders/ScoreBoard/ScoreBoard.h:12: error: conversion from 'const char [17]' to non-scalar type 'QStringList' requested
                [/code]

                [code]
                // the included files sources;

                http://ompldr.org/vYWpycQ/ScoreBoard.cpp
                http://ompldr.org/vYWpycg/ScoreBoard.h
                http://ompldr.org/vYWpycw/ScoreBoard.pro
                http://ompldr.org/vYWpydQ/main.cpp
                [/code]

                Just cannot seem to figure this out, =/. I remember one day, I was working on this Recipes Program, and it worked fine, but can't recall what I did then.

                1 Reply Last reply
                0
                • R Offline
                  R Offline
                  Robbin
                  wrote on 27 Sept 2011, 07:19 last edited by
                  #8

                  You are not declaring the lists properly. Check the usage "here":http://doc.qt.nokia.com/stable/qstringlist.html

                  One of the ways to declare and populate a QStringList is like this
                  @
                  QStringList AFC_North;
                  AFC_North << "Baltimore Ravens" << "Cincinnati Bengals" << "Cleveland Browns" << "Pittsburgh Steelers";
                  @

                  Edit: You can always get how much elements there are in the list with the .size() method (just clarifying, read the docs, it's all there).

                  Edit2: Actually Volker's link has all you need about constructing a string list with good enough examples, more compact then the class reference, you should read that first.

                  1 Reply Last reply
                  0
                  • R Offline
                    R Offline
                    RichardM198030
                    wrote on 27 Sept 2011, 08:01 last edited by
                    #9

                    [quote author="Eus" date="1317107956"]You are not declaring the lists properly. Check the usage "here":http://doc.qt.nokia.com/stable/qstringlist.html

                    One of the ways to declare and populate a QStringList is like this
                    @
                    QStringList AFC_North;
                    AFC_North << "Baltimore Ravens" << "Cincinnati Bengals" << "Cleveland Browns" << "Pittsburgh Steelers";
                    @

                    Edit: You can always get how much elements there are in the list with the .size() method (just clarifying, read the docs, it's all there).

                    Edit2: Actually Volker's link has all you need about constructing a string list with good enough examples, more compact then the class reference, you should read that first.[/quote]

                    Oh oh oh, sorry, I didn't even notice that link, =/. Sorry, but that thing you wrote for me didn't work, =/. Already had a squiggly red line under the NFC_North << ""; part, said declaration expected, I wouldn't even know where to being to fix that.

                    1 Reply Last reply
                    0
                    • R Offline
                      R Offline
                      Robbin
                      wrote on 27 Sept 2011, 08:16 last edited by
                      #10

                      You will have to change quite some things.
                      Begin by moving your QStringList declarations from your .h file to your .cpp file, put them in the constructor, before adding them to the combo boxes.

                      In your scoreboard.cpp, use the .size() method I told you about earlier. And fix your "for" loops! You do not put type for the "i" variable!

                      @
                      for (int i = 0; i < AFC_North.size(); i++)
                      {
                      AFC_North_ComboBox->addItem(AFC_North.at(i));
                      }
                      @

                      Note - I haven't tested your code, that's just the errors I see at a glance, there could be more errors, these were just a few obvious ones.

                      1 Reply Last reply
                      0
                      • R Offline
                        R Offline
                        RichardM198030
                        wrote on 27 Sept 2011, 08:24 last edited by
                        #11

                        [quote author="Eus" date="1317111412"]You will have to change quite some things.
                        Begin by moving your QStringList declarations from your .h file to your .cpp file, put them in the constructor, before adding them to the combo boxes.

                        In your scoreboard.cpp, use the .size() method I told you about earlier. And fix your "for" loops! You do not put type for the "i" variable!

                        @
                        for (int i = 0; i < AFC_North.size(); i++)
                        {
                        AFC_North_ComboBox->addItem(AFC_North.at(i));
                        }
                        @

                        Note - I haven't tested your code, that's just the errors I see at a glance, there could be more errors, these were just a few obvious ones.[/quote]

                        THanks, =). Getting very sleepy to even comprehend anything right now, =P. I'm about to lay down, and have at it later, hehe.

                        1 Reply Last reply
                        0
                        • R Offline
                          R Offline
                          Robbin
                          wrote on 27 Sept 2011, 08:40 last edited by
                          #12

                          Okay, up to you, let us know about the results. And yeah, writing code when you are half asleep always tends to produce bad code - been there, done that :P

                          1 Reply Last reply
                          0
                          • R Offline
                            R Offline
                            RichardM198030
                            wrote on 27 Sept 2011, 08:55 last edited by
                            #13

                            [quote author="Eus" date="1317112814"]Okay, up to you, let us know about the results. And yeah, writing code when you are half asleep always tends to produce bad code - been there, done that :P[/quote]

                            LOL, isn't that always the way? I'm not just fighting with Qt, I'm also fighting with Gentoo, trying to get it to detect my envy VIA sound card, even reconfigured Kernel, updated my bzImage in /boot, and still not working, so irritating, hahaha..

                            1 Reply Last reply
                            0
                            • R Offline
                              R Offline
                              RichardM198030
                              wrote on 28 Sept 2011, 05:35 last edited by
                              #14

                              [quote author="RichardM198030" date="1317111854"]
                              [quote author="Eus" date="1317111412"]You will have to change quite some things.
                              Begin by moving your QStringList declarations from your .h file to your .cpp file, put them in the constructor, before adding them to the combo boxes.

                              In your scoreboard.cpp, use the .size() method I told you about earlier. And fix your "for" loops! You do not put type for the "i" variable!

                              @
                              for (int i = 0; i < AFC_North.size(); i++)
                              {
                              AFC_North_ComboBox->addItem(AFC_North.at(i));
                              }
                              @

                              Note - I haven't tested your code, that's just the errors I see at a glance, there could be more errors, these were just a few obvious ones.[/quote]

                              Awesome, thanks, =). It worked and I see the names in the comboboxes, =). Salute !!
                              [/quote]

                              1 Reply Last reply
                              0
                              • G Offline
                                G Offline
                                goetz
                                wrote on 28 Sept 2011, 10:07 last edited by
                                #15

                                Read the "API docs":http://doc.qt.nokia.com/4.7/qcombobox.html and discover the "convenience methods":http://doc.qt.nokia.com/4.7/qcombobox.html#addItems.

                                And subsequently avoid four-liners, where a one-liner is sufficient:

                                @
                                AFC_North_ComboBox->addItems(AFC_North);
                                @

                                http://www.catb.org/~esr/faqs/smart-questions.html

                                1 Reply Last reply
                                0
                                • R Offline
                                  R Offline
                                  RichardM198030
                                  wrote on 28 Sept 2011, 16:18 last edited by
                                  #16

                                  [quote author="Volker" date="1317204423"]Read the "API docs":http://doc.qt.nokia.com/4.7/qcombobox.html and discover the "convenience methods":http://doc.qt.nokia.com/4.7/qcombobox.html#addItems.

                                  And subsequently avoid four-liners, where a one-liner is sufficient:

                                  @
                                  AFC_North_ComboBox->addItems(AFC_North);
                                  @
                                  [/quote]

                                  Yeah, either way, it's working, =).

                                  1 Reply Last reply
                                  0

                                  1/16

                                  24 Sept 2011, 22:42

                                  • Login

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