Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QTableWidget Column Hold More Than One Item
Qt 6.12 LTS is out! See what's new in the release blog

QTableWidget Column Hold More Than One Item

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 545 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.
  • P Offline
    P Offline
    Phamy1289
    wrote on last edited by
    #1

    I want to get a cell of QTableWidget to hold more than one country name, separated with commas.

    2553a06f-c0ab-4ae8-b763-9f088f7902f6-image.png

    QByteArray answer = reply->readAll();
    QJsonDocument doc = QJsonDocument::fromJson(answer);

    if(doc.isObject())
    {
    QJsonObject root = doc.object();
    QJsonArray countriesArray = root["countries"].toArray();

      for(auto it = root.begin(); it != root.end(); ++it)
      {
            for(const QJsonValue &val : countriesArray)
            {
                  if(it.key() == "countries")
                  {
                        QJsonObject obj = val
                        QString countriesName = obj["name"].toString() + ",  ";
                        mpTable->setItem(mRow, 12, new QTableWidgetItem(countriesName));
                   }
            }
      }
    

    }

    896b2921-4226-4c11-9b80-896216a32d64-image.png

    ec90b56c-21ac-4acf-b15c-5da72db51863-image.png

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mchinand
      wrote on last edited by mchinand
      #2

      Why don't you just append to countryName the values from the iso2 and iso3 elements? You could also create a QStringList with all of the versions of a country name and then use the result of QStringList::join(",") as the value for the table cell.

      1 Reply Last reply
      0
      • P Offline
        P Offline
        Phamy1289
        wrote on last edited by
        #3

        Oh, right. Thanks, I just tried it and it works exactly. Just put it in an if-statement to check if it's at the end of the array so I can see if I need a comma or not.

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt
        • Unsolved