Results 1 to 14 of 14

Thread: My application crashes

  1. #1
    Join Date
    Apr 2009
    Location
    China
    Posts
    127
    Thanks
    30
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question My application crashes

    I have write a application in which user can add QToolButtoninto the UI dynamic. But there is something wrong seriously with my application.
    It often crashes!!
    The following is what I get from the DOS output:

    H:/QTprojects/testdynamic/debug/testdynamic.exe exited with code 1073807364
    in which testdynamic.exe is the application I write.
    Do some guys know the reason?
    Thanks a lot!

  2. #2
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: My application crashes

    You made a programming mistake?

    If you show us some code, maybe we can point you to what that mistake is!

  3. #3
    Join Date
    Apr 2009
    Location
    China
    Posts
    127
    Thanks
    30
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: My application crashes

    Thanks! I agree with you. The following is my code:
    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent)
    2. : QMainWindow(parent), ui(new Ui::MainWindowClass)
    3. {
    4. ui->setupUi(this);
    5.  
    6. }
    7.  
    8. MainWindow::~MainWindow()
    9. {
    10. delete ui;
    11. }
    12.  
    13. bool MainWindow::on_addExeButton_clicked()
    14. {
    15. QString filename = QFileDialog::getOpenFileName(this, tr("Choose the application"), tr("."), tr("Excutable(*.exe)"));
    16. if(filename.isNull())
    17. return false;
    18. //user choose an excutive application
    19. QFileInfo fileInfo(filename);
    20. QIcon icon = seekIcon.icon(fileInfo);
    21. btn->resize( 300, 300);
    22. btn->setIcon(icon);
    23. //if user click the corresponding button, this app will run the chosen application
    24. connect(btn, SIGNAL(clicked()), this, SLOT(callExe()));
    25. buttons.insert(filename, btn);
    26. ui->verticalLayout1->addWidget(btn);
    27. ui->verticalLayout1->addStretch();
    28. }
    29.  
    30. void MainWindow::callExe()
    31. {
    32. qDebug() << "Ready to execute program";
    33. QToolButton *sender = static_cast<QToolButton*>(this->sender());
    34. QString path;
    35. QMapIterator<QString, QToolButton *> i(buttons);
    36. while(i.hasNext())
    37. {
    38. if(i.peekNext().value() == sender)
    39. {
    40. path = i.next().key();
    41. qDebug() << "Find the path!!\n";
    42. break;
    43. }
    44. }
    45.  
    46. if(path.isEmpty())
    47. {
    48. qDebug() << "******File path is null!!";
    49. return;
    50. }
    51. //run the corresponding application
    52. // QObject *parent = NULL;
    53. // QString program = "J:\\2\\exeshan\\Debug\\11.ppt";
    54. QStringList arguments;
    55. arguments << "-style" << "motif";
    56. QProcess *myProcess = new QProcess(this);
    57. myProcess->start(path, arguments);
    58. connect(myProcess, SIGNAL(error(QProcess::ProcessError)), this, SLOT(badProcess(QProcess::ProcessError)));
    59. qDebug() << "SLOT successfully!" << endl;
    60. }
    61.  
    62. void MainWindow::badProcess(QProcess::ProcessError error)
    63. {
    64. qDebug() << "Process error!!" << error;
    65. }
    To copy to clipboard, switch view to plain text mode 

    in this application, I want to run the other applications through click the buttons in the QMainWindow.

  4. #4
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: My application crashes

    as for me you should use qobject_cast instead of static_cast and then check if QToolButton * sender is NULL or not. Next thing is that you can change the key with value in map - that would make starting app much simpler. Here is my code doing similar thing. Check if it is not crashing or tell me what should I do to crash it :]
    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. #include <QFileInfo>
    4. #include <QFileIconProvider>
    5. #include <QIcon>
    6. #include <QFileDialog>
    7. #include <QProcess>
    8.  
    9. MainWindow::MainWindow(QWidget *parent)
    10. : QMainWindow(parent), ui(new Ui::MainWindow)
    11. {
    12. ui->setupUi(this);
    13. }
    14.  
    15. MainWindow::~MainWindow()
    16. {
    17. delete ui;
    18. }
    19.  
    20. void MainWindow::on_actionAdd_application_triggered()
    21. {
    22. QString filename = QFileDialog::getOpenFileName(this, tr("Choose the application"), qApp->applicationDirPath(), tr("Excutable(*.exe)"));
    23. if(filename.isNull())
    24. return;
    25.  
    26. QFileInfo fileInfo(filename);
    27. QIcon icon = seekIcon.icon(fileInfo);
    28. btn->resize( 300, 300);
    29. btn->setIcon(icon);
    30.  
    31. connect(btn, SIGNAL(clicked()), SLOT(slotExecute()));
    32. m_map.insert(btn, filename);
    33. ui->verticalLayout_2->addWidget(btn);
    34. }
    35.  
    36. void MainWindow::slotExecute()
    37. {
    38. QToolButton * btn = qobject_cast<QToolButton *>(sender());
    39. if (!btn)
    40. return;
    41.  
    42. QString filename = m_map.value(btn);
    43. QProcess::startDetached(filename);
    44. }
    To copy to clipboard, switch view to plain text mode 
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

  5. The following user says thank you to faldzip for this useful post:

    sophister (27th April 2009)

  6. #5
    Join Date
    Apr 2009
    Location
    China
    Posts
    127
    Thanks
    30
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: My application crashes

    Thanks!! Uh, I am wondering if it is not suitable to use the C++ static_cast<> or daynamic_cast<>. I am wondering what's the difference between them two and the qobject_cast<>.
    Next thing is, I do not think it is necessary in the
    Qt Code:
    1. void MainWindow::slotExecute()
    To copy to clipboard, switch view to plain text mode 
    to check if the sender is NULL, because if there is no sender, this SLOT won't be called, right?

  7. #6
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: My application crashes

    Quote Originally Posted by sophister View Post
    Next thing is, I do not think it is necessary in the
    Qt Code:
    1. void MainWindow::slotExecute()
    To copy to clipboard, switch view to plain text mode 
    to check if the sender is NULL, because if there is no sender, this SLOT won't be called, right?
    you can call this slot by yourself somwhere in your code, so this check is needed.
    note:
    ...Slots are normal C++ functions and can be called normally; their only special feature is that signals can be connected to them.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  8. #7
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: My application crashes

    Quote Originally Posted by sophister View Post
    Thanks!! Uh, I am wondering if it is not suitable to use the C++ static_cast<> or daynamic_cast<>. I am wondering what's the difference between them two and the qobject_cast<>.
    qobject_cast is using meta information for correct casting of Qt's objects.
    read this for more info.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  9. #8
    Join Date
    Apr 2009
    Location
    China
    Posts
    127
    Thanks
    30
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: My application crashes

    Yeah!!
    I have found the mistake!!
    It hides in the following code:
    Qt Code:
    1. QMapIterator<QString, QToolButton *> i(buttons);
    2. while(i.hasNext())
    3. {
    4. if(i.peekNext().value() == sender)
    5. {
    6. path = i.next().key();
    7. qDebug() << "Find the path!!\n";
    8. break;
    9. }
    10. }
    To copy to clipboard, switch view to plain text mode 
    if the first value in the iterator does not equals the sender, the loop will continue forever!! So the solution is to add such code as:
    Qt Code:
    1. QMapIterator<QString, QToolButton *> i(buttons);
    2. while(i.hasNext())
    3. {
    4. // qDebug() << "Enter the while loop successfully!";
    5. if(i.peekNext().value() == sender)
    6. {
    7. path = i.next().key();
    8. qDebug() << "Find the path!!\n";
    9. break;
    10. }
    11. else
    12. {
    13. i.next(); // without this, the loop may continue forever!!!!!
    14. }
    15. }
    To copy to clipboard, switch view to plain text mode 


    But I've got another question, following is your code to start another application:
    Qt Code:
    1. QProcess::startDetached(filename);
    To copy to clipboard, switch view to plain text mode 

    Are there some different effects between your way and mine? The following is my way to start it:
    Qt Code:
    1. QStringList arguments;
    2. arguments << "-style" << "motif";
    3. QProcess *myProcess = new QProcess(this);
    4. myProcess->start(path, arguments);
    To copy to clipboard, switch view to plain text mode 

    Thanks a lot!!

  10. #9
    Join Date
    Apr 2009
    Location
    China
    Posts
    127
    Thanks
    30
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: My application crashes

    yes, you are right. I forgot that it can be called somewhere by I.
    thanks

  11. #10
    Join Date
    Apr 2009
    Location
    China
    Posts
    127
    Thanks
    30
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: My application crashes

    So I think it will work if I use the ISO C++ dynamic_cast or static_cast.
    The doc says the qobject_cast does not require RTTI.
    what do you think is not suitable if we use dynamic_cast or static_cast?
    By the way, you get up so early!!

  12. #11
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: My application crashes

    Quote Originally Posted by sophister View Post
    So I think it will work if I use the ISO C++ dynamic_cast or static_cast.
    The doc says the qobject_cast does not require RTTI.
    what do you think is not suitable if we use dynamic_cast or static_cast?
    for objects which inherit from QObject it's better to use qobject_cast.
    Quote Originally Posted by sophister View Post
    By the way, you get up so early!!
    my local time is 09:09 am
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  13. The following user says thank you to spirit for this useful post:

    sophister (27th April 2009)

  14. #12
    Join Date
    Apr 2009
    Location
    China
    Posts
    127
    Thanks
    30
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: My application crashes

    Thanks.
    I thought your local time is six o'clock.
    uh, my local time is 2 : 15 p.m.

  15. #13
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: My application crashes

    Quote Originally Posted by sophister View Post
    But I've got another question, following is your code to start another application:
    Qt Code:
    1. QProcess::startDetached(filename);
    To copy to clipboard, switch view to plain text mode 
    in this case a process will start as
    Unix: The started process will run in its own session and act like a daemon.

    Windows: Arguments that contain spaces are wrapped in quotes. The started process will run as a regular standalone process.
    in this case
    Quote Originally Posted by sophister View Post
    Are there some different effects between your way and mine? The following is my way to start it:
    Qt Code:
    1. QStringList arguments;
    2. arguments << "-style" << "motif";
    3. QProcess *myProcess = new QProcess(this);
    4. myProcess->start(path, arguments);
    To copy to clipboard, switch view to plain text mode 

    Thanks a lot!!
    you don't lose communication with a process, i.e. you can handle signals readyReadStandardError, readyReadStandardOutput etc.
    so, if you don't need to process signals then you can use only static methods for starting processes.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  16. The following user says thank you to spirit for this useful post:

    sophister (27th April 2009)

  17. #14
    Join Date
    Apr 2009
    Location
    China
    Posts
    127
    Thanks
    30
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: My application crashes

    Oh, I see.
    thank you, very very much!!

Similar Threads

  1. Replies: 1
    Last Post: 30th March 2009, 22:25
  2. Replies: 3
    Last Post: 3rd March 2009, 12:24
  3. dll + application
    By fpujol in forum Qt Programming
    Replies: 11
    Last Post: 15th April 2007, 18:37
  4. Replies: 5
    Last Post: 27th May 2006, 13:44

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.