Solved Open file via double-click
-
@JonB said in Open file via double-click:
@UnitScan said in Open file via double-click:
Yes, the app crashes if I try to launch it from the command line, while launching it via Explorer it works fine.
And earlier you wrote:
but when I open them "externally" with the double click the program crashes.
How are these two statements consistent?
Let me explain: if I double click on the exe file via Explorer, it starts regularly. If I launch it via cmd it crashes instead.
If I double click on any audio file, the program opens but crashes immediately -
If I launch it via cmd it crashes instead.
So what happens if you launch it from Creator debugger, like I asked?
Put in some
qDebug()
statements to see if it gets as far as yourmain
, and then where. -
I did as you asked, and this is the result:
#include "mainwindow.h" #include <QApplication> #include <QTimer> #include <stdio.h> #include <iostream> int main(int argc, char *argv[]) { for (int i = 0; i < argc; ++i) { std::cout << "The " << i << "th argument is " << argv[i] << std::endl; } QApplication a(argc, argv); MainWindow w; QTimer::singleShot(0, & w, SLOT(initialize())); w.show(); return a.exec(); }
Application output in Debug mode:
ASSERT failure in QList<T>::at: "index out of range", file C:/Qt/Qt5.14.0/5.14.0/mingw73_32/include/QtCore/qlist.h, line 571
-
@UnitScan
You must run it in the debugger, from Creator, not just compile for debug. Then when theASSERT
is hit and the debugger traps it you will find there is a stack trace window. That will show theqlist.h
line, and then trace back through all the levels to some line in your code, which is what was being executed when theASSERT
happened.And please use
qDebug()
for debug output statements, notstd::cout
.Separately, since you persist in using
SLOT()
, all bets are off as to how that might behave at runtime.And finally, I hope you know what you're doing with seemingly initializing on a timer, seems hokey to me. You sure you understand exactly when
initialize()
will be executed, compared to other initializations (like in constructors) you are doing? -
@UnitScan hi
@UnitScan said in Open file via double-click:
argv[i]
this is the kind of code that can cause "index out of range" error.
you can use
https://doc.qt.io/qt-5/qcoreapplication.html#arguments
or
https://doc.qt.io/qt-5/qcommandlineparser.html
to retrive the arguments -
@LeLev
In this case he is usingmain(argc, argv)
correctly. The point (to me) is:ASSERT failure in QList<T>::at: "index out of range", file C:/Qt/Qt5.14.0/5.14.0/mingw73_32/include/QtCore/qlist.h, line 571
I don't see how that can arise from his
argc
/argv
/std::cout
loop. Has to be aQList
involved! -
@JonB well, I found the origin of the assert failure
void MainWindow::performStyles() { QString theme = settings->value("app/theme").toString(); settings->beginGroup("themes"); QStringList colors = settings->value(theme).toString().split(","); settings->endGroup(); QString stylesheet = ( "QSlider::groove:horizontal {" "border: 1px solid #999999;" "height: 5px;" "background: qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 #B1B1B1, stop:1 #c4c4c4);" "border-radius: 3px;" "}" "QSlider::handle:horizontal {" "background: qlineargradient(x1:0, y1:0, x2:1, y2:1, stop:0 #b4b4b4, stop:1 %3);" "border: 1px solid #5c5c5c;" "width: 16px;" "margin: -2px 0;" "border-radius: 3px;" "}" "QSlider::handle:horizontal:hover {" "background: qlineargradient(x1:0, y1:0, x2:1, y2:1, stop:0 #FFFFFF, stop:1 %3);" "border: 1px solid #808080;" "width: 16px;" "margin: -2px 0;" "border-radius: 3px;" "}" "QSlider::add-page:horizontal {" "background: #c0c0c0;" "border-radius: 3px;" "margin: 0 -1px;" "}" "QSlider::sub-page:horizontal {" "background: qlineargradient(x1:0, y1:0, x2:1, y2:1, stop:0 #b4b4b4, stop:1 %3);" "border-radius: 3px;" "}" "QFrame#data {" "border: 1px solid gray;" "border-radius: 5px;" "background-color: %1;" "}" "QLabel#display {" "font-size: 20px;" "color: %2;" "padding-bottom: 1px;" "background-color: %1;" "}" "QLabel#kbps, QLabel#hz, QLabel#channels {" "font-size: 10px;" "text-align: center;" "color: %2;" "}" "ScrollText {" "font-size: 14px;" "text-align: center;" "color: %2;" "}" "QTableView {" "border: 1px solid gray;" "border-radius: 5px;" "background-color: %1;" "color: %2;" "}" "QTableView::item:selected {" "border-radius: 3px;" "background-color: %2;" "color: %1;" "}" "QScrollBar#vscrollbar:vertical {" "border: 1px solid grey;" "border-radius: 3px;" "background: %1;" "width: 15px;" "margin: 0px 0 0px 0;" "}" "QScrollBar#vscrollbar::handle:vertical {" "background: qlineargradient(x1:0, y1:0, x2:1, y2:1, stop:0 #b4b4b4, stop:1 %1);" "border: 1px solid #5c5c5c;" "width: 16px;" "min-height: 16px;" "border-radius: 3px;" "}" "QScrollBar#vscrollbar::add-line:vertical {" "border: 0px solid grey;" "background: #32CC99;" "height: 0px;" "subcontrol-position: bottom;" "subcontrol-origin: margin;" "}" "QScrollBar#vscrollbar::sub-line:vertical {" "border: 0px solid grey;" "background: #32CC99;" "height: 0px;" "subcontrol-position: top;" "subcontrol-origin: margin;" "}" "QScrollBar#vscrollbar::add-page:vertical, QScrollBar::sub-page:vertical {" "background: none;" "}" "QListWidget::item:selected {" "background-color: none;" "border: 3px solid grey;" "}" "QListWidget::item:selected { background: transparent; }" "QListWidget::item:selected { border: 1px solid red; }" ); this->setStyleSheet(stylesheet.arg(colors.at(0),colors.at(1),colors.at(2))); }
This seems to be the line causing the error
this->setStyleSheet(stylesheet.arg(colors.at(0),colors.at(1),colors.at(2)));
My ini settings file is written like this
[themes] Red="#C21212,#ffffff,#C21212"
I must have misused the QString arg method
-
@JonB yes you are right, my bad.
i thought about argv[i] as a QList for some reason -
@UnitScan
Yourcolors.at()
expressions are fine so long asQStringList colors = settings->value(theme).toString().split(",");
really does returns 3 colors. My guess is that, depending on how you run the program --- e.g do you read from "the current working directory"? or, what is
qDebug() << theme
, orqDebug() << settings->value(theme).toString()
? --- you're not picking up the right thing? Code like yours which relies on 3 elements from thesplit()
should have aQ_ASSERT(colors.count() == 3);
. Anyway, somehow that differs when you run your code in different ways.I hope now that you have found using the debugger helps find such errors!
-
I suppose the problem is bigger: when I run the program via cmd it doesn't even load the icons: https://i.imgur.com/yqvpYnl.png
-
as @JonB pointed out, this is probably related to working directory. Make sure you load your settings from the right path! This might come in handy: https://doc.qt.io/qt-5/qcoreapplication.html#applicationDirPath
-
@sierdzio said in Open file via double-click:
as @JonB pointed out, this is probably related to working directory. Make sure you load your settings from the right path! This might come in handy: https://doc.qt.io/qt-5/qcoreapplication.html#applicationDirPath
Thank you very much!