Juba
4th May 2010, 16:33
Hi all,
I'm trying to build a custom widget that act like a button, but with more feature, like icons and multple labels...
Like any button I'd like to start a "callback" when the button is clicked. I think that the signal slot model does not work for this inter class case.
So I try the following code:
header
class CallButton : public QWidget
{
Q_OBJECT
public:
CallButton(QWidget *parent = 0);
~CallButton();
protected:
void mousePressEvent(QMouseEvent *event);
QGroupBox *groupBox;
QLabel *label;
QLabel *icon;
};
and. cpp
CallButton::CallButton(QWidget *parent)
: QWidget(parent)
{
groupBox = new QGroupBox(this);
label = new QLabel("Label...");
icon = new QLabel;
QIcon fakeIcon = QIcon("eclipse24.png");
QPixmap pixmap = fakeIcon.pixmap(QSize(16, 16), QIcon::Normal, QIcon::On);
icon->setPixmap(pixmap);
QHBoxLayout *mainLayout = new QHBoxLayout;
mainLayout->addWidget(label);
mainLayout->addWidget(icon);
mainLayout->insertStretch(1);
groupBox->setStyleSheet(QString::fromUtf8("\
QGroupBox {\
background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,\
stop: 0 #FFFFFF, stop: 1 #E0E0E0);\
border: 2px solid gray;\
border-radius: 5px;\
}"));
groupBox->resize(180,60);
groupBox->setLayout(mainLayout);
}
void CallButton::mousePressEvent ( QMouseEvent * event )
{
groupBox->setStyleSheet(QString::fromUtf8("\
QGroupBox {\
background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,\
stop: 0 #E0E0E0, stop: 1 #FFFFFF);\
border: 2px solid gray;\
border-radius: 5px;\
}"));
}
CallButton::~CallButton()
{
delete groupBox;
delete label;
delete icon;
}
The application compile and run, but the event is not launched when I left click inside the button (the event is launched when I left click outside the QGroupBox area and anywhere where I right click).
Why the left click does not work?
Thank's in advance to everyone,
Stefano.
I'm trying to build a custom widget that act like a button, but with more feature, like icons and multple labels...
Like any button I'd like to start a "callback" when the button is clicked. I think that the signal slot model does not work for this inter class case.
So I try the following code:
header
class CallButton : public QWidget
{
Q_OBJECT
public:
CallButton(QWidget *parent = 0);
~CallButton();
protected:
void mousePressEvent(QMouseEvent *event);
QGroupBox *groupBox;
QLabel *label;
QLabel *icon;
};
and. cpp
CallButton::CallButton(QWidget *parent)
: QWidget(parent)
{
groupBox = new QGroupBox(this);
label = new QLabel("Label...");
icon = new QLabel;
QIcon fakeIcon = QIcon("eclipse24.png");
QPixmap pixmap = fakeIcon.pixmap(QSize(16, 16), QIcon::Normal, QIcon::On);
icon->setPixmap(pixmap);
QHBoxLayout *mainLayout = new QHBoxLayout;
mainLayout->addWidget(label);
mainLayout->addWidget(icon);
mainLayout->insertStretch(1);
groupBox->setStyleSheet(QString::fromUtf8("\
QGroupBox {\
background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,\
stop: 0 #FFFFFF, stop: 1 #E0E0E0);\
border: 2px solid gray;\
border-radius: 5px;\
}"));
groupBox->resize(180,60);
groupBox->setLayout(mainLayout);
}
void CallButton::mousePressEvent ( QMouseEvent * event )
{
groupBox->setStyleSheet(QString::fromUtf8("\
QGroupBox {\
background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,\
stop: 0 #E0E0E0, stop: 1 #FFFFFF);\
border: 2px solid gray;\
border-radius: 5px;\
}"));
}
CallButton::~CallButton()
{
delete groupBox;
delete label;
delete icon;
}
The application compile and run, but the event is not launched when I left click inside the button (the event is launched when I left click outside the QGroupBox area and anywhere where I right click).
Why the left click does not work?
Thank's in advance to everyone,
Stefano.