views:

157

answers:

1

I have two processes which exange messages each other. Process A is a normal (i.e non-qt) program which sends messages to process B. Process B is a QT GUI application showing received messages into a text box.

I created a customized widget (called ShowMessages) which inherits from QPlainTextEdit and reads messages from a pipe when a timer expires, and appends them in the text box. Code is not really designed like this (which would be pretty bad design I think), but it's just to make things simple here. For reasons I won't tell, process A cannot be shut down while I'm creating the form using qt-designer.

Problem is that while I'm using qt designer (thus selecting the ShowMessages widget and putting it within the window) the widget begins to show messages, even if I'm in the designer. This feature is cool but the problem is that when I save the form, already present messages are saved in .ui file, which results in turn in bad behaviour when I start process B (because process starts showing messages I received during the creation phase).

I could clean the text box just after process B starts, but I think that avoiding messages to be present in the .ui file is much better. What I want is to be able to write code like this for the widget:

if <I'm not in the designer>
    timer = QtCore.QTimer(self)
    QtCore.QObject.connect(timer, QtCore.SIGNAL("timeout()"), self, QtCore.SLOT("on_timer()"));
    timer.start(2000)

Is there an qt function to know if I'm in the designer? Do you think that distinguishing between designer and runtime code is "bad"?

(Sorry for mistakes, but English is not my primary language)

+1  A: 

Maybe your widget should have a flag for whether it's "active" and default it to False so while you're in the designer, it doesn't do anything at all. In code you would set it to active when you want to see the messages. Then you also have the ability to turn it off in other scenarios as well.

But I have to say, it sounds like you're putting "controller" code into a "view" widget which can and probably will spell trouble for you down the road (including the current Qt designer problem you're having now).

Consider reading up on the MVC (model-view-controller) design pattern, if you haven't already.

Update:

To be fair, your question did ask how to detect whether you're in designer :)

http://doc.trolltech.com/4.3/designer-creating-custom-widgets.html#creating-well-behaved-widgets

To give custom widgets special behavior in Qt Designer, provide an implementation of the initialize() function to configure the widget construction process for Qt Designer specific behavior. This function will be called for the first time before any calls to createWidget() and could perhaps set an internal flag that can be tested later when Qt Designer calls the plugin's createWidget() function.

According to the doc, you basically could set your "inDesignerFlag" to true in the initialize() function of your widget. Then detect that flag where required in your widget's code.

Chris Cameron