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. Selected item from QListWidget
QtWS25 Last Chance

Selected item from QListWidget

Scheduled Pinned Locked Moved General and Desktop
19 Posts 3 Posters 8.9k Views
  • 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.
  • D Offline
    D Offline
    david_aa
    wrote on last edited by
    #1

    Hi all,

    How can I get a selected item of a QListWidget?

    I tried this:
    @
    ListWidget::ListWidget(QWidget *parent)
    : QDialog(parent)
    {
    ui.setupUi(this);

    // List all "Package Types" in a listWidget
    string a_app;
    vector<string> ListPacketDB;
    m_objDataPck.GetPacketTypes(a_app, ListPacketDB);
    for (int i=0;i<ListPacketDB.size();i++)
    {
    ui.m_listPacket->addItem(ListPacketDB[i].c_str());
    }

    // List all "stations" in a listWidget
    vector<string> ListStationsDB;
    m_objDataPck.GetGroundStations(ListaEstacoesDB);
    for (int i=0;i<ListStationsDB.size();i++)
    {
    ui.m_listGS->addItem(ListStationsDB[i].c_str());
    }
    }

    void ListWidget::OnBnClickedOk()
    {

    QString auxItens;
    QList<QListWidgetItem*> selItemList = ui.m_listPacket->selectedItems(); // Get the total index of the selected items.

    m_LineCommand+= ";"; // delimiter
    if(selItemList.size() > 0)
    {
    int *pSels = new int[selItemList];
    ui.m_listPacket->selectedItems(selItemList,pSels);
    for (int i = 0; i < selItemList.size();i++)
    {
    //pSels[i] this is the index of the item that was selected
    ui.m_listPacket->text(pSels[i],auxItens);
    m_LineCommand+= auxItens;
    if(i < selItemList.size() - 1)m_LineCommand+= ","; // delimiter
    }
    delete [] pSels;
    }
    else
    m_LineCommand+="ALL";
    }
    @

    But this error occurs:
    "This Error...":http://pastebin.com/z7567PcX

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      line 35 is false. On line 29, you are doing it correctly. What are you trying to achieve exactly ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • A Offline
        A Offline
        ambershark
        wrote on last edited by
        #3

        It looks like you are trying to get the list widget item text for each selected item in the list widget.

        If you let us know why/what you are trying to accomplish there may be a better way to go about it. But, with that said, here is the code you need to accomplish what you are trying to do:

        @
        QList<QListWidgetItem *> itemList = ui.m_listPacket->selectedItems();

        // does this need to be cleared in between "button clicks"?
        // if so, m_LineCommand.clear();
        m_LineCommand += ";";

        for (int i=0;i<itemList.size();++i)
        {
        m_LineCommand += itemList.at(i)->text();
        if (i < itemList.size())
        m_LineCommand += ";";
        }
        @

        My L-GPL'd C++ Logger github.com/ambershark-mike/sharklog

        1 Reply Last reply
        0
        • D Offline
          D Offline
          david_aa
          wrote on last edited by
          #4

          Hi SGaist

          I need to get all the selected values ​​in QListWidget but this errors occurs.

          I'm having these errors:

          In this line: ui.m_listPacket->selectedItems(selItemList,pSels);
          1>.\ListWidget.cpp(35) : error C2064: term does not evaluate to a function taking 0 arguments

          This error too: error C2440: 'initializing' : cannot convert from 'QList<T>' to 'unsigned int'

          error C2660: 'QListWidget::selectedItems' : function does not take 2 arguments

          this line: ui.m_listPacket->text(pSels[i],auxItens);
          error C2039: 'text' : is not a member of 'QListWidget'

          And this error: error C2679: binary '+=' : no operator found which takes a right-hand operand of type 'QString' (or there is no acceptable conversion)

          Thanks,

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            ambershark's code is the solution to your problem.

            As for the errors you are getting, please re-read the documentation of the functions

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            0
            • D Offline
              D Offline
              david_aa
              wrote on last edited by
              #6

              Hi ambershark,

              I did what you said, but the error continues. Please see

              @QList<QListWidgetItem *> itemList = ui.m_listPacote->selectedItems();

              // if so, m_LineCommand.clear();
              m_LineCommand += ";";
               
              for (int i=0;i<itemList.size();i++)
              {
                 m_LineCommad += itemList.at(i)->text();
                 if (i < itemList.size())
                    m_LineCommand += ";";
              }@
              

              Errors:

              1- In this line: connect(ui.m_buttonOk(), SIGNAL(clicked(bool)), this, SLOT(OnBnClickedOk())); * (Error: error C2064: term does not evaluate to a function taking 0 arguments* )

              2- In this line: m_LineCommad += itemList.at(i)->text(); (Error: error C2679: binary '+=' : no operator found which takes a right-hand operand of type 'QString' (or there is no acceptable conversion))

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

                What is the type of m_LineCommand ? I guess not a QString

                ui.m_buttonOk() is wrong, it's ui.m_buttonOk, m_buttonOk is not a function.

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                0
                • D Offline
                  D Offline
                  david_aa
                  wrote on last edited by
                  #8

                  Hi SGaist

                  So what to do is the following.
                  I have a form with fields: DateInitial and DateEnd; TimeInitial and TimeEnd; listPackt; ListID; listStation; listComm

                  And I need to get these values ​​in a command line that I will use later. So I created the variable "string m_LineCommand" which will store these values ​​as follows:

                  Example: "04/09/2002 08:00:00.000; 05/09/2002 12:33:33.000; <list pckType>; <list pckID>; <list ET>; <list ComTYpe>;"

                  1 Reply Last reply
                  0
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    Since you are using Qt, why don't you use QString for m_LineCommand ?

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply
                    0
                    • D Offline
                      D Offline
                      david_aa
                      wrote on last edited by
                      #10

                      Hi SGaist,

                      I'm rewriting a project developed in C + + using MFC. I'm also new to Qt, but now reading I saw what was going wrong. The correct is QString

                      Another question please.

                      When I comment the lines:

                      @
                      connect (ui.m_buttonOk, SIGNAL (clicked (bool)), this, SLOT (OnBnClickedOk ()));

                       QList <QListWidgetItem *> ITEMLIST = ui.m_listPacote-> selectedItems ();
                           
                       
                           / / If so, m_LineCommand.clear ();
                           m_LineCommand + = ";"
                           
                           for (int i = 0; i <itemList.size (); i + +)
                           {
                              m_LineCommad itemList.at = + (i) -> text ();
                              if (i <itemList.size ())
                                 m_LineCommand + = ";"
                           }@
                      

                      The Qt does not lock and run my form, but keep the uncommented lines to load the form and try to select an option, the form Qt crashes. What can it be?

                      1 Reply Last reply
                      0
                      • SGaistS Offline
                        SGaistS Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on last edited by
                        #11

                        Your question is not clear at all, can you rephrase it please ?

                        Interested in AI ? www.idiap.ch
                        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                        1 Reply Last reply
                        0
                        • D Offline
                          D Offline
                          david_aa
                          wrote on last edited by
                          #12

                          Hi,

                          I managed to solve, first I went "Clean Solution", then I went on "Rebuild Solution"

                          But can you please help me with this part of the code.

                          I need to get the Initial Date, End Date and concatenate them. I did this:

                          @// Declaration of Variables
                          string strDate, strInitialDate, strFinalDate;
                          string a_pckIdList;

                          QString csDate;
                          CSPTDateTime DateTimeInitial;
                          CSPTDateTime DateTimeFinal;

                          // Recovering the Initial Date of the calendar

                          DateTimeInitial = ui.m_initialDate->GetDateSelect();
                          if (DateTimeInitial.GetYMD(strDate,2) == SPTERRO) return;
                          strInitialDate = strDate.substr(0, 11);@

                          But this errors occurs: "ERRORS": http://pastebin.com/AKtB8zdC

                          1 Reply Last reply
                          0
                          • SGaistS Offline
                            SGaistS Offline
                            SGaist
                            Lifetime Qt Champion
                            wrote on last edited by
                            #13

                            I can't since I don't know what's in CSPTDatePopup.h

                            Interested in AI ? www.idiap.ch
                            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                            1 Reply Last reply
                            0
                            • D Offline
                              D Offline
                              david_aa
                              wrote on last edited by
                              #14

                              Hi,

                              This CSPTDatePopup.h is an internal class that I need to use. But my question is how should I do to get the Initial Date and End Date and concatenate.

                              Can you help me, please?

                              Thanks,

                              1 Reply Last reply
                              0
                              • SGaistS Offline
                                SGaistS Offline
                                SGaist
                                Lifetime Qt Champion
                                wrote on last edited by
                                #15

                                You might need to use it, but it seems that the error comes from it.

                                Interested in AI ? www.idiap.ch
                                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                1 Reply Last reply
                                0
                                • D Offline
                                  D Offline
                                  david_aa
                                  wrote on last edited by
                                  #16

                                  Hi,

                                  But In this Line: DateTimeInitial = ui.m_initialDate->GetDateSelect();
                                  error C2039: 'GetDateSelect' : is not a member of 'QCalendarWidget'

                                  Can you hep me?

                                  1 Reply Last reply
                                  0
                                  • SGaistS Offline
                                    SGaistS Offline
                                    SGaist
                                    Lifetime Qt Champion
                                    wrote on last edited by
                                    #17

                                    Beside telling you that you are calling a function that doesn't exist in QCalendarWidget… You might be rather looking for selectedDate

                                    Interested in AI ? www.idiap.ch
                                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                    1 Reply Last reply
                                    0
                                    • D Offline
                                      D Offline
                                      david_aa
                                      wrote on last edited by
                                      #18

                                      Hi,

                                      Can you help me?

                                      In this line is occurs this error: (Line) DateTimeInitial = ui.m_initialDate->selectedDate();

                                      (Error)
                                      error C2679: binary '=' : no operator found which takes a right-hand operand of type 'QDate' (or there is no acceptable conversion)

                                      This line: if (DateTimeInitial.GetYMD(strDate,2) == SPTERRO) return;
                                      This error: error C2664: 'CSPTDateTime::GetYMD' : cannot convert parameter 1 from 'QString' to 'std::string &'

                                      This line: strInitialDate = strDate.substr(0, 11);
                                      error C2039: 'substr' : is not a member of 'QString'

                                      1 Reply Last reply
                                      0
                                      • SGaistS Offline
                                        SGaistS Offline
                                        SGaist
                                        Lifetime Qt Champion
                                        wrote on last edited by
                                        #19

                                        Please, read the documentation of the classes you are using. You can't use = to automagically put a QDate in a QString. You can't auto convert QString in string.

                                        First thing to do: cleanup your code. You are uselessly mixing Qt with some other library. If you need to return a string somewhere, fine, but do the conversion at the last time, there's no need to go back and forth between different library types like that.

                                        Interested in AI ? www.idiap.ch
                                        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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