Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Exception on setStyleSheet(). Can't figure why.

Exception on setStyleSheet(). Can't figure why.

Scheduled Pinned Locked Moved General and Desktop
10 Posts 3 Posters 5.1k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    nikitablack
    wrote on last edited by
    #1

    Hello.

    I have pretty big project and unfortunately I can't provide small reproducible piece of code. But I glad to hear ideas. I have a 2 main windows. When I open a new one I delete previously opened. And at some point I'm calling:

    qApp->setStyleSheet(styleSheetStr);
    

    And this line fails with exception read access violation at: 0x0.
    The call stack after calling of the method looks like this:

    QJsonObject::value	Qt5Cored		0x66f535ca	
    QUndoGroup::cleanChanged	Qt5Widgetsd		0x6427bb1c	
    QUndoGroup::cleanChanged	Qt5Widgetsd		0x643cfcfd	
    QUndoGroup::cleanChanged	Qt5Widgetsd		0x643c49ac	
    QUndoGroup::cleanChanged	Qt5Widgetsd		0x6429fc8b	
    SkinManager::setSkin	SkinManager.cpp	30	0x13f410965	
    

    So the actual fail happens in QJsonObject::value. What can cause this behavior or how can I debug a problem?

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      '''
      With no code, it is just random guesses/questions, but sometimes that does work too :)

      where does styleSheetStr lives?
      Can it run out of scope.

      if you remove the line, it will not fail ?

      the qApp remains valid and non zero at all times?

      if you do not delete the previously opened main window (for test) does it still fail ?

      1 Reply Last reply
      1
      • A Offline
        A Offline
        ambershark
        wrote on last edited by
        #3

        Since QApplication knows about all the top level widgets, my guess here is that you are deleting one of the QMainWindow objects and your application doesn't realize it happened by the time you try to set a style sheet.

        If you are doing something like delete mainWindow1 then qApp->setStyleSheet() that may be your issue. Try changing to a mainWindow1->deleteLater() and see if the crash goes away.

        Also definitely check into @mrjj's suggestions, especially making sure qApp is a legit pointer.

        You can also try calling the change to stylesheets later, after the event loop has had a chance to clean up the main window that was destroyed. You can do this with a QTimer::singleShot. This of course assumes the deleteLater() doesn't fix the issue.

        My L-GPL'd C++ Logger github.com/ambershark-mike/sharklog

        1 Reply Last reply
        1
        • N Offline
          N Offline
          nikitablack
          wrote on last edited by
          #4

          Thank you guys. The problem exactly related to deleting a window. Without deleting it everything works ok. Unfortunately nothing from the above helped and with minimal example the issue does not reproduces. But now I know where to dig. Thanks.

          1 Reply Last reply
          0
          • N Offline
            N Offline
            nikitablack
            wrote on last edited by
            #5

            An update. After investigating I found the problem. But I have a lack of knowledge in the field.
            For my project I need borderless windows. I'm using a QWinWidgets from qt-solutions . It uses some undocumented functions to obtain window handle (on Windows). And my problem arises when I'm destroying this window with DestroyWindow(). I don't know what's happens inside, but somehow QApplication, HWND and stylesheets relates to each other.

            mrjjM 1 Reply Last reply
            0
            • N nikitablack

              An update. After investigating I found the problem. But I have a lack of knowledge in the field.
              For my project I need borderless windows. I'm using a QWinWidgets from qt-solutions . It uses some undocumented functions to obtain window handle (on Windows). And my problem arises when I'm destroying this window with DestroyWindow(). I don't know what's happens inside, but somehow QApplication, HWND and stylesheets relates to each other.

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @nikitablack

              This is pure speculation:

              In the QwinWidgets code for its event handler it has

                case QEvent::Close:
                  	::SetActiveWindow(hParent);
              	if (w->testAttribute(Qt::WA_DeleteOnClose))
              	    deleteLater();
              	break;
              

              so if you set that flag on your window, and do not delete it yourself. will it still crash ?

              window->setAttribute(Qt::WA_DeleteOnClose);
              

              That way the QwinWidget controls the deletion since it seems to manage it.

              Also, if you are only using QwinWidget for a borderless window, you could maybe just use a normal window and

              window->setWindowFlags ( Qt::Popup |  Qt::Dialog | Qt::FramelessWindowHint );
              

              (might need to adjust flags)

              1 Reply Last reply
              1
              • N Offline
                N Offline
                nikitablack
                wrote on last edited by
                #7

                @mrjj, thank you for reply. I can't use just flags with a normal window for several reasons. One of them - I have to keep standard window functionality like minimize/maximize anumations, aero shake, aero snap. The QEvent::Close also not works - the program simply never reaches this line.

                mrjjM 1 Reply Last reply
                0
                • N nikitablack

                  @mrjj, thank you for reply. I can't use just flags with a normal window for several reasons. One of them - I have to keep standard window functionality like minimize/maximize anumations, aero shake, aero snap. The QEvent::Close also not works - the program simply never reaches this line.

                  mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @nikitablack

                  Ok, so QWinWidgets does provide more than borderless. Just checking.

                  Looking In qapplication.cpp file, at
                  setStyleSheet, i see nothing that looks like anything from the call stack dump. ?

                  It does call QApplication::setStyle(QStyle *style) which will
                  iterate over all Widgets.

                  So if you deleted QWinWidgets still to be found, that would explain it.
                  But why should it be there still.

                  you could check it fast with

                   foreach (QWidget *widget, QApplication::allWidgets())
                        qDebug() <<  widget->objectName();
                  
                  1 Reply Last reply
                  1
                  • N Offline
                    N Offline
                    nikitablack
                    wrote on last edited by
                    #9

                    @mrjj
                    Yes, with QWinWidget I'm able to grab native handle and do whatever I want.
                    I did the check and didn't found anything suspicious.
                    One thing that helped me - is a call to qApp->style()->unpolish(qApp);. After this line I'm able to call qApp->setStyleSheet() without an exception.

                    mrjjM 1 Reply Last reply
                    0
                    • N nikitablack

                      @mrjj
                      Yes, with QWinWidget I'm able to grab native handle and do whatever I want.
                      I did the check and didn't found anything suspicious.
                      One thing that helped me - is a call to qApp->style()->unpolish(qApp);. After this line I'm able to call qApp->setStyleSheet() without an exception.

                      mrjjM Offline
                      mrjjM Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      @nikitablack
                      ok, so its not due to it sticking around in widget list.

                      so

                       qApp->style()->unpolish(qApp)
                      

                      is kinda a "fix" even we never know the real reason.

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved