Crash when deleting QGraphicsProxyWidget

Currently I'm working on a track-based event editor. I'm building this editor with a QGraphicsScene. I use multiple QGraphicsViews for showing tracks and midi-events. Wave files etc...

To rename a track I was trying to show a QLineEdit directly above the track-name and commit the result when editing was completed.

Adding it to the scene like this:

QLineEdit* lineEdit = new QLineEdit("My Text");
// a few connected signals for listening to editingFinished and enterPressed
QGraphicsProxyWidget* proxyWidget = graphicsScene->addWidget( lineEdit )

While I was trying to delete a simple QGraphicsProxyWidget from my QGraphicsScene
it resulted in a crash. (Deleting proxyWidget also delete's it's QLineEdit child)

delete proxyWidget;

The stack-trace shows something about focus function withing Qt.
I tried several things like deleteLater. Calling clearFocus myself.
It all did not work..

The solution that worked for me is the following:

void editingFinishedHandler()
{
  scene()->removeItem( proxyWidget );
  proxyWidget->deleteLater();
}


(My previous solution, that's inferior to the one above: )

[code language="cpp"]
void editingFinishedHandler()
{
  proxyWidget->setWidget(0);  // < inferior solution, please see solution above
  proxyWidget->deleteLater();      
  lineEdit->deleteLater();    
}

QThread deleteLater not called on application Quit

Threading in Qt is not very hard (so it seems).
But there's no definitive way to use use QThread. Look at the different articles and opinions.

My opinion is to be pragmatic and consider the context and situation of your problem, and just use what's best for it. :)

In this article I would like to describe a problem I had with threading. I've got a gui application that uses several threads. It's an application with multiple tabs, and every tab has got several (always running) threads. These threads are event-queue threads and are waiting on messages (jobs) they need to process.

My application uses memory-leak detection so when an object doesn't get deleted I get an horrible message at the end.

I've made the threads self-destroy when you call quit:

  connect( thread, SIGNAL(finished()), thread, SLOT(deleteLater()) );

When you close the application directly via (Alt-F4 windows, or Cmd-Q on Mac) I destroy the different tabs an stop the threads.

I process this in the signal QApplication::aboutToQuit:

  thread->quit();
  thread->wait()

Calling quit ends the eventQueue of the thread and emits a finished() signal to the event queue..

But now there's a problem. I'm in the aboutToQuit signal. And it seems that the finished signal is placed on the event queue.

But the eventQueue isn't processed any more. The application exits the exec loop:

  qApp->exec();

When my application know exits I've got a problem the QThread is not deleted.

The finished() signal is placed on the qApp event queue, but isn't processed.

I've solved it by processing the eventQueue after my exec call.

int result =  qApp->exec();
int counter = 0;

while( qapp->eventDispatcher()->processEvents( QEventLoop::ExcludeUserInputEvents ) && counter < 25  ) 
{         
  ++counter;
}

I've created a loop to allow pending events to be processed. To be sure it will quit I've added a counter, but in my situation the loop is processed 2 times:

  • The first time: finished() is processed, and deleteLater() is posted to the eventQueue
  • The second time the deleteLater() is processed and the my QThread is deleted

Now I've finally got rid of that annoying memory leak message :)

** The non-deletion of QThread is not a real big issue, but the situation I describe here also applies to other slots I've connected to the finished() signal of the thread. These slot's aren't called without the method described above.

Solve Parallels Virtual Machines Mac Sloooowness

Sometimes I need to boot Window on my Mac. I'm using Parallels 7 for booting my BootCamp Windows 7 installation.
I know there's a new version of Parallels, but I'm not buying a new one for now. Did this since version 4, and it seems as if Parallels got slower every version. This could also be the fault of windows.

Booting windows 7 on my parallels machine slowed down my complete Machine drasticly!!
I played with options optimize for Mac, or for Host. No difference.
I trimmed down the looks of my Windows 7, so it looks like pre-XP.
I even blamed Avira Antivirus..

After doing some testing I found out it is not the CPU that's loaded, but the bottleneck seemed to be IO..

Windows Vista and Windows 7 make use of a superfetch function the preloads data in memory smartly when booting the machine. This is NOT what I want on my Virtual Machine!
Please do not use up all my memory...

I found a BIG gain in performance (of My Mac) by disabling the service:

  • Control Panel
  • Administrative Tools
  • Services
  • Stop the service "Superfetch" en set it to Manual
    • This worked great for me!
      (Btw I also disabled the indexing of my complete C drive)

A ruby idea

In ruby you can check ranges via de 'range' language construct. For example: (12..23).include?( value).
But how about chaining '<', '<=', '=>' and '>' operators.

For example. currently in Ruby I must write the following

if 10 < x && x < 15
  # code
end

Why isn't it possible to write

if 10 < x < 15
  # code
end

Well in fact it is possible with some hacks :)
The example below is only valid for Fixnum, but it describes the possibilities:

The hack is to just simply return the right hand. In Ruby this shouldn't be a problem, because every object/values is true except false and nil.

class Fixnum
  def <(val)
    super ? val : false
  end
  def <=(val)
    super ? val : false
  end
  def >(val)
    super ? val : false
  end
  def >=(val)
    super ? val : false
  end
end

To make this work the FalseClass should also support these operators, and simply return false to make the complete expression return false if one of them fails:


class FalseClass
  def <(val)
    false
  end
  alias :<= :<
  alias :> :<
  alias :>= :< 
end

So 10 < x just returns 'x' on succes and returns false on error. [code language="ruby"] if 10 < x < 15 # code end [/code] I'm wondering what could/would be the 'problem' by using this construct? Is there a specific reason this has not been implemented this way? BTW: just found a 'nice' implementation for this construct on: http://refactormycode.com/codes/1284-chained-comparisons-in-ruby

[:<, :>, :<=, :>=].each do |operator|
  [Float, Fixnum, Comparable].each do |klass| 
    klass.class_eval {
      alias_method("__#{operator}__", operator)
      define_method(operator) do |operand|
        send("__#{operator}__", operand) and operand
      end 
    }
  end
  FalseClass.send(:define_method, operator) { false }
end

My ruby external-encoding hack

Reading (text)-files from disk is very easy in Ruby.

content = IO.read( "filename.txt" )

When you are trying to do something with this content you can get in trouble.

content.split(",")  # => invalid byte sequence in UTF-8

I've setup my environment very nicely, so Ruby treats external files as UTF-8. The trouble begins when you are trying to handle files that are encoded in the ISO-8859-1 or CP-1252 format and Ruby thinks they are UTF-8.

To accept both UTF-8 and ISO-8858-? formats I've implemented the following hack:

  def convert_to_utf8(content)
    if content.valid_encoding?
      content
    else
      content.force_encoding("ISO-8859-1").encode("UTF-8")
    end    
  end

  # reading the content:
  content = convert_to_utf8 IO.read( "filename.txt" ) 

This hack works for me because the text-files I use are in one of those formats.