Solved 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?
-
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::Invalidalmost the solution ;) instead of the description I am getting the name of the port back.
@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 -.- "?
-
@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 applicationthen you MUST also have
ui->serialPortInfoListBox->itemData(idx).toStringList();else there is no list to take out.
-
@bask185 said in Question about Qcombo box < crash issue:
I do not know what a stack trace is -.- "
Qt Creator: http://doc.qt.io/qtcreator/creator-debug-mode.html#viewing-call-stack-trace
Visual Studio: https://msdn.microsoft.com/en-gb/library/windows/hardware/hh439516(v=vs.85).aspx
XCode: http://stackoverflow.com/a/26341275 -
-
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.
-
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
-
@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 becauselist
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:
-
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 -_-*
-