PDA

View Full Version : Add signals to qTextEdit



emp1953
27th August 2019, 21:24
I have implemented a 3rd party LED widget called tled. It suggests promoting a qTextEdit widget to tled because of some of the methods that tled needs to inherit from qTextEdit.

Now, unfortunately I find that I need the ability to click on the promoted LED to initiate some processing. Of course qTextEdit does not emit a clicked() or pressed() signal.

I guess my only option, if I stick with the qTextEdit is to attach mouse functions to qTextEdit. How do I do that? or can I add a clicked() signal to my LED?

I am using RHEL6 and Qt4.8.

Thanks for any help

emp1953

Lesiok
28th August 2019, 08:30
How about this (https://www.linux-apps.com/p/1132137/) ?

anda_skoa
28th August 2019, 09:53
While it sounds very strange that you would use a multi line text input widget as the base for a LED widget, the mechanism to add signals to your class is always the same.

1) add a "signals" section to the class declaration
2) add declarations for the signals you want to have
3) overwrite the protected virtual event handler methods that you want to react to and emit your signals when appropriate

Cheers,
_

emp1953
28th August 2019, 14:55
While it sounds very strange that you would use a multi line text input widget as the base for a LED widget, the mechanism to add signals to your class is always the same.

1) add a "signals" section to the class declaration
2) add declarations for the signals you want to have
3) overwrite the protected virtual event handler methods that you want to react to and emit your signals when appropriate

Cheers,
_

One of the methods that TLED needs to inherit is setValue(). Can you suggest a better widget to promote to that has both setValue() and clicked() or pressed() or mouseReleased() etc.

anda_skoa
28th August 2019, 15:41
One of the methods that TLED needs to inherit is setValue().

This is getting even more weird: QTextEdit does not have a setValue() method.

Do you mean LED as in Light Emitting Diode? I.e. a control light of sorts?

Or does that mean something entirely different in your context and which is why you need the capability to display formatted multi line text?



Can you suggest a better widget to promote to that has both setValue() and clicked() or pressed() or mouseReleased() etc.

All widgets have mousePressed() and mouseReleased() event handlers, these are part of the QWidget API.

Cheers,
_

emp1953
28th August 2019, 17:39
This is getting even more weird: QTextEdit does not have a setValue() method.

Do you mean LED as in Light Emitting Diode? I.e. a control light of sorts?

Or does that mean something entirely different in your context and which is why you need the capability to display formatted multi line text?



All widgets have mousePressed() and mouseReleased() event handlers, these are part of the QWidget API.

Cheers,
_


Light emitting diode.
When I try to connect mousePressed to a slot I get an error saying that mousePressed is not found.


This is getting even more weird: QTextEdit does not have a setValue() method.

Do you mean LED as in Light Emitting Diode? I.e. a control light of sorts?

Or does that mean something entirely different in your context and which is why you need the capability to display formatted multi line text?



All widgets have mousePressed() and mouseReleased() event handlers, these are part of the QWidget API.

Cheers,
_

Sorry I misspoke. it is not setValue, it is setText.

d_stranz
28th August 2019, 18:32
I have implemented a 3rd party LED widget called tled.

Isn't your "tled" class already derived from something like QPushButton? It should already have the necessary methods for setting text, etc.

I think you may be a bit confused between "promoting" a widget that you are using in Qt Designer and "deriving" a widget from some base class that has functionality you want to use. They are totally different concepts.

In Qt Designer, you can lay out a user interface window (dialog, widget, whatever) and use some suitable widget as a placeholder in the GUI to stand in for a custom widget like your tled widget. After you finish the layout, you tell Qt Designer to "promote" the standard widget you have used to be your custom widget instead. All this does in practice is to update the ui file to replace the class name of the Qt widget with the class name of the custom widget and add some more UI code to import the correct header file for the custom widget when the UIC compiler runs.

When you want to use Qt Designer promotion and you want to be able to use Qt Designer to set default values for your custom widget, you generally choose a Qt widget that has properties with the same names as those for your custom widget. Qt Designer will create UI code for the stock Qt widget you have chosen that just happens to continue to work after promotion because the custom widget has the same property. Perhaps this is why the authors of your tled widget suggested using QTextEdit as the placeholder before promotion.

Promotion does not mean that you can magically add new properties to your custom widget by choosing a Qt widget with those properties and then promoting it. Your custom widget is what it is and you can't change it through some UI designer switcheroo.

What you can do is to derive a new C++ class that uses your custom widget as a base class. You can then add whatever functionality you want to this new class because you're in charge. If you name new properties for this class to be the same as those used by a Qt widget (a "text" property, for example), then you can trick Qt Designer into generating the code to support that property in your new widget by choosing the appropriate Qt widget, setting the property in Designer, then promoting the Qt widget to your new widget. You have not done anything to change the programming interface for either widget. All you have done is to tell Qt Designer, "for the purposes of setting this property value, treat my custom widget as though it was this standard Qt widget instead".


When I try to connect mousePressed to a slot I get an error saying that mousePressed is not found.

"mousePressed()" is a Qt widget event, not a signal. You cannot connect events to slots. This will either result in a compiler error or a Qt runtime warning (depending on how the code is written). If you want to handle a mousePressed() event and be able to use that to call a slot method, then the simplest procedure is to derive a new class, add a custom signal (mousePressedSignal() for example), implement an override of the mousePressed() event, and in that handler emit your new signal. Once you have that in place, you can connect this new signal to whatever slot you want.