How Can I make Clickable Label in My Project
-
My Baseclass is Mainwindow
i shown some review but they are using QLabel as a Baseclass
i am try to slove this issue using connect() function
help me:
Thanks in Advanced
-
@Ketan__Patel__0011 Example :
Clickable QLabelthis link will be active for 2 days;
you need to use promote widget after add widget header and class name (**right click options in ui file) -
Hi
Did you try this sample ?
https://wiki.qt.io/Clickable_QLabelIt will make your own QLabel and then you use that in your MainWindow to click on.
-
@mrjj i was used this reference It works good But i want to set click-event only particular label
i was used Connect function for any particular label and run my project now i will click any label it will effect each and every label
-
Well then something went wrong then as it really is pr label.
If you dont connect the label, clicking it will do nothing.- now i will click any label it will effect each and every label
Then there is bug in your code. Connecting one instance of the label
will not affect any other.Please show the code so we can correct it. :)
-
I am Take A Three Label, in my ui
****************** My header files like this *********************
#ifndef MAINWINDOW_H
#define MAINWINDOW_H#include <QMainWindow>
#include <QMouseEvent>
#include <QMessageBox>
#include <QDebug>namespace Ui {
class MainWindow;
}class MainWindow : public QMainWindow
{
Q_OBJECTpublic:
explicit MainWindow(QWidget *parent = nullptr);
~MainWindow();private:
Ui::MainWindow *ui;signals:
void clicked();protected:
void mousePressEvent(QMouseEvent* event);};
#endif // MAINWINDOW_H
****************** My Sources file like this *********************
#include "mainwindow.h"
#include "ui_mainwindow.h"MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect(ui->label,SIGNAL(clicked()),this,SLOT(mousePressEvent()));
}MainWindow::~MainWindow()
{
delete ui;
}void MainWindow::mousePressEvent(QMouseEvent *event)
{
QMessageBox::information(this,"Message","WELCOME");
emit clicked();
}
when i click any label message will be raised
but i want this message for any only label1
please Help Me for slove this problem
-
Hi,
It has absolutely nothing to do with your labels. Wherever you'll click on the MainWindow it will show that message.
If you only what to act on a specific QLabel either make that one use the custom class suggested by @mrjj or use an event filter to act only on that QLabel.
-
@Ketan__Patel__0011 said in How Can I make Clickable Label in My Project:
no i will just click on label then message is raised
I would dobut that since you connect the qlabel to a slot which is called on every mouse click as explained in the documentation. Use another slot name.
-
-
@Ketan__Patel__0011 said in How Can I make Clickable Label in My Project:
Can you make One Example For Me ?
Of what? creating a member function with a different name than 'mousePressEvent()'? What's the problem?
-
Hi
Several things are wrong
If you are using a plain QLabel, it wont have a clicked signal.
Only using the class in the link it will have it. a plain QLabel cannot be clicked.So lets say you ARE using the new class then this can work.
connect(ui->label,SIGNAL(clicked()),this,SLOT(LabelClicked())); void MainWindow::LabelClicked() { QMessageBox::information(this,"Message","WELCOME"); }
The reason it shows the message anywhere you click currently is due to the fact you added
void MainWindow::mousePressEvent(QMouseEvent *event)
for the MAINWINOW do it will trigger anywhere you click on it.
So its only useful if you where to click anywhere on the whole Mainwindow and not
on any labels. -
Well did you use the ClickableLabel or not ?
-
Hi
there is no full project.
the wiki page shows what to put in the .cpp and .h.However, if its also due to you not understanding how to use it.
I can make you a test project this evening if that helps? -
@Ketan__Patel__0011 Example :
Clickable QLabelthis link will be active for 2 days;
you need to use promote widget after add widget header and class name (**right click options in ui file) -
**@anil_arise hey Brother Thank you So much
It really Good Way My Problem is solved
Again Thank You So Much**
-
@anil_arise Can you share this link again plzzzzz