Selected item from QListWidget



  • 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


  • Lifetime Qt Champion

    Hi and welcome to devnet,

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



  • 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 += ";";
    }
    @



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


  • Lifetime Qt Champion

    ambershark's code is the solution to your problem.

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



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


  • Lifetime Qt Champion

    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.



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


  • Lifetime Qt Champion

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



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


  • Lifetime Qt Champion

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



  • 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


  • Lifetime Qt Champion

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



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


  • Lifetime Qt Champion

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



  • Hi,

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

    Can you hep me?


  • Lifetime Qt Champion

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



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


  • Lifetime Qt Champion

    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.


Log in to reply
 

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