Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Question about Qcombo box < crash issue



  • I have made a menu window with which I can input settings for the serial port it looks like
    0_1490621080549_upload-87248ba9-2548-4e14-a2c9-bdd10cb1adbf

    The thing works but I want a small upgrade. Under the 'serial port' textlabel I want to display the description of the device, in my current case a ch340 serial ttl converter. I copy pasted most of this code of the terminal example app and made some alterations.

    I use this code:

    void Setup::on_serialPortBox_activated(const QString &arg1)
    {
        z = ui->serialPortBox->currentIndex(); //<< works!
        QStringList list = ui->serialPortBox->itemData(z).toStringList(); // crashes application
        ui->descriptionLabel->setText(list[z]);// << works
    }
    

    This code gets activated when I select a serial port (when I use the combo box) but the middle line of code crashes my program when I use the combo box. I'd like know why it happens and how I can fix it.



  • @VRonin I was looking on the QStringList page ...

    I was doing that in an other function

    for (QSerialPortInfo &info : infos) {
            QStringList list;
    
            list << info.portName();
            ui->serialPortBox->addItem(list.first(), list);
        }
    

    So I was stuffing port names in the combo box, but not the description because that is not what I want. The descriptions are to be printed under the combobox in a text label.

    So that function made me thinking, I made a new private QStringList variable called 'list2' and in the same function in which I fill the combobox I also fill 'list2' with dem descriptions

    for (QSerialPortInfo &info : infos) {
            QStringList list;
    
            list << info.portName();
            list2 << info.description(); // < new line
            ui->serialPortBox->addItem(list.first(), list);
        }
    

    so now I only had to do.

    void Setup::on_serialPortBox_activated(const QString &arg1)
    {
        z = ui->serialPortBox->currentIndex();
    
        ui->descriptionLabel->setText(list2[z]);
    }
    

    And voila:
    USB 2.0


  • Lifetime Qt Champion

    maybe
    ui->serialPortBox->itemData(z)
    return QVariant::Invalid ?



  • How do you set the item data for each item in the QComboBox? Does each item in the QComboBox have its own description text? If so, why isn't each item's data just a string and your code like this:

    QString list = ui->serialPortBox->itemData(z).toString();
    ui->descriptionLabel->setText(list);
    

    I'm also confused how the last line is working if your program is crashing before it.



    • check that z is within range
    • Are you setting a model in the combobox? I suspect the segfault comes from there, can you post stack trace?


  • I thought I posted the following yesterday.. but I did not.
    @mrjj said in Question about Qcombo box < crash issue:

    ui->serialPortBox->itemData(z)
    return QVariant::Invalid

    almost the solution ;) instead of the description I am getting the name of the port back.

    0_1490683288537_upload-7cd88127-57fd-4b40-997c-80b0d4dd1eee

    @mchinand said in Question about Qcombo box < crash issue:

    How do you set the item data for each item in the QComboBox? Does each item in the QComboBox have its own description text?

    ui->baudRateBox->addItem(QStringLiteral("9600"), QSerialPort::Baud9600);
        ui->baudRateBox->addItem(QStringLiteral("19200"), QSerialPort::Baud19200);
        ui->baudRateBox->addItem(QStringLiteral("38400"), QSerialPort::Baud38400);
        ui->baudRateBox->addItem(QStringLiteral("115200"), QSerialPort::Baud115200);
        ui->baudRateBox->setCurrentIndex(3);
    

    In the example software the following code is used:

     QStringList list = ui->serialPortInfoListBox->itemData(idx).toStringList();
        ui->descriptionLabel->setText(tr("Description: %1").arg(list.count() > 1 ? list.at(1) : tr(blankString)));
    

    But the example uses for functions than I need, so to keep it simple I do not copy paste..everything. Anyways I could net get this code to work :(

    @VRonin said in Question about Qcombo box < crash issue:

    • check that z is within range
    • Are you setting a model in the combobox? I suspect the segfault comes from there, can you post stack trace?

    Z is in range (gives me back 0-2) and I do not know what a stack trace is -.- "?


  • Lifetime Qt Champion

    @bask185 said in Question about Qcombo box < crash issue:

    Just to be 100% clear

    if you use
    QStringList list = ui->serialPortBox->itemData(z).toStringList(); // crashes application

    then you MUST also have
    ui->serialPortInfoListBox->itemData(idx).toStringList();

    else there is no list to take out.







  • I'll reuse @Wieland standard reply: "The image-upload feature on our forum is broken, you might see the picture but other users don't. Please upload your image to a image hoster of your choice (e.g. postimage.org) and embed the pic here with the following markup: ![alternate text](image-url). See also How to insert image on this forum and Hitchhiker's Visual Guide to the Qt Forum."



  • @VRonin I'm sorry, didn't know that.
    alt text



  • That's the assembly, what we are after is the content of the table at the bottom left just below "Debbugger" with headers "Level", "Function","File","Line"



  • @VRonin this is the only information I can find, is this of any help??

    1 ZeqRK7QStringS1_   0x68aed806 
    2 ??                 0x2d630a0  
    3 ??
    


  • This addItem code is for a different QComboBox then in your original post (baudRateBox instead of serialPortBox). What is the addItem code for serialPortBox? The second parameter of addItem() is the UserRole data that will be retrieved by itemData() (by default, you can select a different role if you want, however).



  • I have more combo boxes, it was just an example of how I put things in them


  • Moderators

    @bask185 You need to build and run the debug version in order to get the stack trace with detailed information. If you do this and then post that information we can tell you where your crash is.

    Based on the release stack trace you are crashing in QString. We don't know where yet but if you repost the debug build stack trace we will. It should tell us line and file.



  • ASSERT failure in QList<T>::operator[]: "index out of range", file ../../../Qt5.7.0/5.7/gcc_64/include/QtCore/qlist.h, line 545

    index out of range issue it seems, I verified that z is always within the range (0-2)

    1   __GI_raise                                                                                                                 raise.c       54  0x7ffff5b81428 
    2   __GI_abort                                                                                                                 abort.c       89  0x7ffff5b8302a 
    3   QMessageLogger::fatal(const char *, ...) const                                                                                               0x7ffff653ff4e 
    4   qt_assert_x(const char *, const char *, const char *, int)                                                                                   0x7ffff653b821 
    5   QList<QString>::operator[]                           // << this the one                                                        qlist.h       545 0x427f8d       
    6   Setup::on_serialPortBox_activated                                                                                          setup.cpp     115 0x424c04       
    7   Setup::qt_static_metacall                                                                                                  moc_setup.cpp 116 0x4297aa       
    8   Setup::qt_metacall                                                                                                         moc_setup.cpp 183 0x4299ee       
    9   QMetaObject::activate(QObject *, int, int, void * *)                                                                                         0x7ffff6746700 
    10  QComboBox::activated(QString const&)                                                                                                         0x7ffff77d4c65 
    11  ??                                                                                                                                           0x7ffff77d6126 
    12  ??                                                                                                                                           0x7ffff77d88e9 
    13  ??                                                                                                                                           0x7ffff77de91d 
    14  QMetaObject::activate(QObject *, int, int, void * *)                                                                                         0x7ffff6746056 
    15  ??                                                                                                                                           0x7ffff77d528b 
    16  QCoreApplicationPrivate::sendThroughObjectEventFilters(QObject *, QEvent *)                                                                  0x7ffff671e853 
    17  QApplicationPrivate::notify_helper(QObject *, QEvent *)                                                                                      0x7ffff76dc505 
    18  QApplication::notify(QObject *, QEvent *)                                                                                                    0x7ffff76e3808 
    19  QCoreApplication::notifyInternal2(QObject *, QEvent *)                                                                                       0x7ffff671eae0 
    20  QApplicationPrivate::sendMouseEvent(QWidget *, QMouseEvent *, QWidget *, QWidget *, QWidget * *, QPointer<QWidget>&, bool)                   0x7ffff76e27bf 
    ... <More>                                                                                                                                                      
    
    


  • problem looks to be in: ui->descriptionLabel->setText(list[z]); almost surely because list is empty as I never saw you setting the user role to a QStringList anywhere in your code



  • @VRonin that is probably because I cannot find the words 'user' or 'role' anywhere on the doc.qt page and I don't understand how I can succesfully use a QStringClass for my purpose.

    @all Nobody knows how I can display the description of my serial port???



  • @bask185 said in Question about Qcombo box < crash issue:

    I cannot find the words 'user' or 'role' anywhere on the doc.qt page

    😕 http://doc.qt.io/qt-5/qcombobox.html#insertItem

    Nobody knows how I can display the description of my serial port???

    we do and we tried to explain you how. you have to save it in the data of the combobox



  • @VRonin I was looking on the QStringList page ...

    I was doing that in an other function

    for (QSerialPortInfo &info : infos) {
            QStringList list;
    
            list << info.portName();
            ui->serialPortBox->addItem(list.first(), list);
        }
    

    So I was stuffing port names in the combo box, but not the description because that is not what I want. The descriptions are to be printed under the combobox in a text label.

    So that function made me thinking, I made a new private QStringList variable called 'list2' and in the same function in which I fill the combobox I also fill 'list2' with dem descriptions

    for (QSerialPortInfo &info : infos) {
            QStringList list;
    
            list << info.portName();
            list2 << info.description(); // < new line
            ui->serialPortBox->addItem(list.first(), list);
        }
    

    so now I only had to do.

    void Setup::on_serialPortBox_activated(const QString &arg1)
    {
        z = ui->serialPortBox->currentIndex();
    
        ui->descriptionLabel->setText(list2[z]);
    }
    

    And voila:
    USB 2.0



  • not how I would have done it but good enough for rock and roll

    P.S.
    I'd have done:

    for (const QSerialPortInfo &info : infos) 
            ui->serialPortBox->addItem(info.portName(), info.description());
    

    and then, in the constructor, added:
    connect(ui->serialPortBox,static_cast<void(QComboBox::*)(int)>(&QComboBox::currentIndexChanged),[this](){ui->descriptionLabel->setText(ui->serialPortBox->currentData().toString();});

    no need for QStringLists



  • dat looks rather.. exremely complicated compared to what I did O.o vague C++ syntax sometimes. Besides I really must avoid the use of pointers whenever possible, pointers are not good for my mental health -_-*



  • ok, I guess


  • Moderators

    @bask185 said in Question about Qcombo box < crash issue:

    Besides I really must avoid the use of pointers whenever possible, pointers are not good for my mental health

    Lol, you may want to consider something other than C++. ;) I felt that way when I was first learning, but if you continue to feel that way, you really need a language that does it for you. Java, C#, python, tons of options. Python will even let you use Qt. :)



  • I was having a perfectly fine working application in processing. I shook dat code right out of my sleeve. Easy OO programming, no difficult vague words such as 'public' or 'private', no pointers, some easy global variables which I can use in every class I make. Truly processing is by far the easiest program ever :D

    And do you know what it takes to export an application to Linux? Ctrl shift E, check Linux, press Export applications and BAM it works! Java ftw! But.... app was too heavy for my rpi... freakin java -_-". Now it takes me more than a week to crosscompile for the rpi. I had to install ubuntu on my Pc to find out I had only one 1GB space left so I had to delete my windows OS.... I do kinda regret I did not learn python and Tkinter first...

    But the things I learn.. priceless


  • Moderators

    @bask185 Yea java is just so slow and heavy. That's why I could never get into it.

    I use an ide (CLion) that is written in java and it is so slow compared to the other one I use that is written in C++ (slickedit). I like CLion's features, but the java engine is just so slow. It's almost unusable on large projects, meanwhile slickedit has no speed issues.

    Another anecdote.. I spun up an AWS micro server the other day to run upsource from jetbrains. It needed > 2GB in RAM just to start the application because of the java and cassandra dependency. If I wrote a similar application to upsource in something faster like C++ it would take maybe 50mb to start, not >2gb, lol. I killed the AWS server as I didn't want to pay for the bigger server just to mess around with upsource and installed it locally on a server in my office.

    Morale of the story, java is great for easy, it does everything for you, programming, but it is awful when it comes to actually using the software that is produced, lol.


Log in to reply