QLabel - mouseReleaseEvent to parent - HTML label



  • Hi,

    I have a subclass of QDialog (called CDialog) that has a member of type: QLabel m_Text;

    CDialog has mouseReleaseEvent() where it detects if the user right or left clicks the dialog.

    My QLabel has this set:

    this->m_Text.setText("This is a sample text");
    

    If I do:

    
    this->m_Text.setTextFormat(Qt::PlainText);
    

    Everything works fine. If I click on the text, it still relays the signal to the parent (so I catch the event).

    But if I do:

    this->m_Text.setTextFormat(Qt::RichText);
    

    The signal never gets relayed up to the parent. Somehow, if it's HTML, the QLabel catches the signal and blocks it. I can still press under/above the text string, and my CDialog will get the signal.
    I'm guessins this is because it scans for <a>-tags?

    I've tried:

    this->m_Text.setOpenExternalLinks(false);
    

    But that did little to nothing (saw it on another forum).

    So, how can I tell QLabel to always send the signal upwards - even if it's RichText?

    Thanks in advance.

    Best regards,
    Anton



  • I really have no idea about this, but you might be able to accomplish this by inheriting QLabel and reimplementing mouseReleaseEvent. Some thoughts are to possibly use QMouseEvent::ignore(), QCoreApplication::sendEvent(), or QCoreApplication::postEvent(). As I said, I don't really know; It's just a thought.



  • Hi JordanHarris,

    Thanks for your response!

    I thought so too - but I really want to avoid making yet another subclass, I was hoping that there would be some sort of flag or setting that I've missed that could essentially solve this.

    But if there is none, then I'll have to make a new class inheriting it.

    Thanks for your thoughts, though :)

    Best regards,
    Anton


  • Qt Champions 2016

    Hi
    I tried a default project with a dialog and a label using rich text.

    I could not get it to steal the clicks.
    Dialog always got the click.

    So I wonder, besides setting
    setTextFormat(Qt::RichText);

    do you do anything else to make this happen?



  • Hi @mrjj

    Thank you for taking time and checking this.

    That is weird indeed. This is how I setup my dialog (this being my CDialog : public QDialog)

    		QString message = "This is a sample text";
    		this->setModal(false);
    		this->setWindowFlags(Qt::FramelessWindowHint|Qt::SplashScreen|Qt::WindowStaysOnTopHint);
    
    		m_Text.setObjectName("label_NotificationText");
    		m_Text.setStyleSheet("QLabel#label_NotificationText { color: #DDDDDD; }");
    		m_Text.setWordWrap(true);
    		m_Text.setText(message);
    		m_Text.setParent(this);
    		//m_Text.setTextFormat(Qt::RichText);
    

    Then I have this function to capture the clicks:

    void mouseReleaseEvent ( QMouseEvent * event ) {
    	if(event->button() == Qt::RightButton) {
    		this->deleteLater();
    	}
    	else if(event->button() == Qt::LeftButton) {
    		// Call the function we wanted
    		emit onNotificationPressed(this);
    		this->deleteLater();
    	}
    }
    

    Which never gets called if I press the QLabel m_Text (if it's HTML-enabled).

    I don't know if this matters, but I show the dialog using ->show();

    Best regards,
    Anton


  • Qt Champions 2016

    Super
    That helped a lot - I could reproduce it.

    You can disable it with
    m_Text.setTextInteractionFlags(Qt::NoTextInteraction);

    You might need to set some flags if u need copy paste etc.
    This completely removes any interaction.



  • @mrjj

    Wow, super. That actually solved it.

    Thank you so much for your response, your time and your help. I really appreciate it.

    Thanks.

    Best regards,
    Anton


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.