Solved Open file via double-click
-
I set my Qt application as the default one for audio files. But when I try to open a file by double clicking on it, the program opens and crashes immediately.
Is there anything I need to add to my code for this not to happen?
This is the part about initializing the programmain.cpp
#include "mainwindow.h" #include <QApplication> #include <QTimer> #include <stdio.h> int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; QTimer::singleShot(0, & w, SLOT(initialize())); w.show(); return a.exec(); }
mainwindow.cpp
void MainWindow::initialize() { QStringList filenames = QCoreApplication::arguments(); if (filenames.size() > 0) { for (int i=0; i < filenames.size(); i++) { QString name(filenames.at(i)); qDebug() << "argument" << name; } } }
-
The code you posted looks ok (if a bit unconventional :D). Crash probably happens somewhere else.
-
@UnitScan
As @sierdzio says --- ought be OK.But, do yourself a favor, do not use old style
SIGNAL()
/SLOT()
macros, use new styleconnect()
s --- https://wiki.qt.io/New_Signal_Slot_Syntax ! -
When I open the files from my program everything is ok, but when I open them "externally" with the double click the program crashes.
-
@UnitScan said in Open file via double-click:
When I open the files from my program everything is ok
But do you run your program from Qt Creator, or independently in filesystem (by clicking .exe)?
Qt Creator sets some env. vars to make life easier. But to open an app without QtC, you need to deploy it first.
One other thing which can help with crashes: compile and run the application with address sanitizer. It will show you where you are accessing invalid memory.
-
@sierdzio said in Open file via double-click:
@UnitScan said in Open file via double-click:
When I open the files from my program everything is ok
But do you run your program from Qt Creator, or independently in filesystem (by clicking .exe)?
Qt Creator sets some env. vars to make life easier. But to open an app without QtC, you need to deploy it first.
One other thing which can help with crashes: compile and run the application with address sanitizer. It will show you where you are accessing invalid memory.
Yes, I run it independently. The .exe file is located in a folder where there are all the dlls necessary for its operation. For the address sanitazer, could you give me some tips?
-
@UnitScan said in Open file via double-click:
The .exe file is located in a folder where there are all the dlls necessary for its operation
How do you know? How did you deploy your app to this location?
Start a cmd.exe, go to the directory and start your app to see if it crashes. -
@UnitScan
Have you run your program outside of Qt Creator? Have you tried passing the same command-line arguments from the command-line as it would if you double-clicked on a file in Explorer? -
@Christian-Ehrlicher said in Open file via double-click:
@UnitScan said in Open file via double-click:
The .exe file is located in a folder where there are all the dlls necessary for its operation
How do you know? How did you deploy your app to this location?
Start a cmd.exe, go to the directory and start your app to see if it crashes.@JonB yes, I deployed my app following this.
Yes, the app crashes if I try to launch it from the command line, while launching it via Explorer it works fine.
If I try to open the files from the command line, the program still crashes -
@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?
Well that sounds strange, doesn't it? The only difference should be what Explorer passes on the command-line. Maybe you've written your program to expect the right thing(s) in
argv
, I don't know. Have you done this? When you do not run from File Explorer, what arguments are you passing it?If it crashes from command-line, won't it do so from Creator debugger, where you can see the stack trace?
Otherwise, put some
qDebug()
statements into it (starting from the very first line inmain()
) to see where it's getting to. If anywhere. -
@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