Arrays And an error when trying to declare them



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



  • 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 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, =).



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



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



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



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



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



  • [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]



  • 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 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, =).


Log in to reply
 

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