Solved QLabel and right click
-
I have a QLabel called backgroundCalibration whose text I have set to "<a>Some text</a>"
I did:
ui->backgroundCalibration->setVisible(true); ui->backgroundCalibration->setTextInteractionFlags(Qt::LinksAccessibleByMouse); ui->backgroundCalibration->setForegroundRole(QPalette::Link); void StackingParameters::on_backgroundCalibration_linkActivated(const QString & str) { str; // // Show the popup menu // backgroundCalibrationMenu->exec(QCursor::pos()); }
When I display the Widget containing this label the text is displayed as "Some Text" and is displayed in Blue as I expected.
However when I
right-clickleft-click the text nothing happens! What crucial step did I miss?TIA
David -
@Perdrix
You cannot use "<a>Some text</a>" to expect a linkActivated signal.
You must set some text as the link: "<a href="any text">Some text</a>", or the text won't even look like linked. -
@Perdrix said in QLabel and right click:
However when I right-click the text nothing happens!
What do you expect to happen?
-
I expect the slot on_backgroundCalibration_linkActivated() to be driven.
David
-
@Perdrix Shouldn't it happen on left mouse button click?
-
Sorry I meant when I LEFT click, nothing happens
David (I'll edit the original text to correct that)
-
@Perdrix It seems like you're relying on the auto-connect feature, right? I mean you do not connect the signal to slot explicitly. You could try to connect by yourself to see whether it makes a difference.
-
Yes, I'm using auto-connect and I don't get any warnings about Qt being unable to connect the signals to the slots (as I did when I originally tried to use on_backgroundCalibration_clicked() - that definitely got a complaint.
-
@Perdrix
Please at least put aqDebug()
/breakpoint inside your slot to verify being called, just in case it is being called and you (somehow) can't see the menu! -
@Perdrix The auto-connect feature is not that reliable. I would try with explicit connect. If it complains then post your connect call here and the error.
-
I added:
connect(ui->backgroundCalibration, SIGNAL(linkActivated(const Qstring &)), this, SLOT(on_backgroundCalibration_linkActivated()));
to the ctor code just after the ui->SetupUI() call.
I have a breakpoint inside the slot code at the line that is intended to raise the menu, but it is never reached.
David
-
@Perdrix
If that is a paste of your code,Qstring
is mis-spelt. You would get help if that is not connecting the slot if you changed to new-style signal-slot syntax, which would benefit you in the long run. -
@Perdrix
You cannot use "<a>Some text</a>" to expect a linkActivated signal.
You must set some text as the link: "<a href="any text">Some text</a>", or the text won't even look like linked. -
-
Yes I spotted that just after I posted and fixed it.
So the code now reads:
connect(ui->backgroundCalibration, SIGNAL(linkActivated(const QString &)), this, SLOT(on_backgroundCalibration_linkActivated(const QString &)));
and received no nastygrams about non-existent signals. Sadly the breakpoint is still not reached.
PS what's the new format for connecting the signals and slots? My book is strictly old school Qt4
David
-
@Perdrix Did you read what @Bonnie wrote?
https://wiki.qt.io/New_Signal_Slot_Syntax/de -
@Bonnie Silly question - why can't I just use <a>Text</a>? It is after all a valid minimal HTML anchor?
I shall change the code immediately to use the format you suggested.
D.
-
@Perdrix
Because<a>Text</a>
is not clickable in HTML, it just defines an anchor point user can jump to (via#
on URL), see e.g. https://stackoverflow.com/questions/10510191/valid-to-use-a-anchor-tag-without-href-attribute. -