Hi,
I am having some issue in coding in QT for Linux.
The scenario is that I have to create some pushbuttons, but I don't want QT native pushbutton, and I have images for both Pressed and Normal states of the button.
I am using QIcon, QStylesheet and QPaintEvent overriding to display the image.
Here is the code structure
header file:
{
private:
public:
{
normalImage=nImg;
}
{
pressedImage=pImg;
}
{
pmapUp.
setMask(pmapUp.
createMaskFromColor(QColor(255,
0,
255)));
pmapDown.
setMask(pmapDown.
createMaskFromColor(QColor(255,
0,
255)));
if (this->isDown())
{
icon.
addPixmap(pmapDown,
QIcon::Normal,
QIcon::On);
icon.
addPixmap(pmapDown,
QIcon::Normal,
QIcon::Off);
}
else
{
icon.
addPixmap(pmapUp,
QIcon::Normal,
QIcon::Off);
}
this->setIcon(icon);
}
};
{
public:
explicit MainWindow
(QWidget *parent
= 0);
~MainWindow();
private:
NewButton *pb;
}
class NewButton:public QPushButton
{
private:
QString normalImage;
QString pressedImage;
public:
NewButton() : QPushButton() {}
NewButton(QWidget *w) : QPushButton(w) {}
void setNormalImage(QString nImg)
{
normalImage=nImg;
}
void setPressedImage(QString pImg)
{
pressedImage=pImg;
}
void paintEvent(QPaintEvent *event)
{
QPixmap pmapUp(normalImage);
pmapUp.setMask(pmapUp.createMaskFromColor(QColor(255,0,255)));
QPixmap pmapDown(pressedImage);
pmapDown.setMask(pmapDown.createMaskFromColor(QColor(255,0,255)));
QIcon icon;
if (this->isDown())
{
icon.addPixmap(pmapDown, QIcon::Normal, QIcon::On);
icon.addPixmap(pmapDown, QIcon::Normal, QIcon::Off);
}
else
{
icon.addPixmap(pmapUp, QIcon::Normal, QIcon::On);
icon.addPixmap(pmapUp, QIcon::Normal, QIcon::Off);
}
this->setIcon(icon);
QPushButton::paintEvent(event)
}
};
class MainWindow:public QMainWindow
{
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
NewButton *pb;
}
To copy to clipboard, switch view to plain text mode
cpp file
MainWindow
::MainWindow(QWidget *parent
) : {
setGeometry(10,10,800,600);
centralWidget->setObjectName(QStringLiteral("centralWidget"));
centralWidget->setGeometry(0,0,780,580);
pb=new NewButton(centralWidget);
pb->setGeometry(20,20,116,68);
pb->setFlat(true);
pb
->setIconSize
(QSize(116,
68));
pb->setStyleSheet("QPushButton{border:0px; outline: none;}");
pb->show();
}
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent)
{
setGeometry(10,10,800,600);
centralWidget = new QWidget(this);
centralWidget->setObjectName(QStringLiteral("centralWidget"));
centralWidget->setGeometry(0,0,780,580);
pb=new NewButton(centralWidget);
pb->setGeometry(20,20,116,68);
pb->setFlat(true);
pb->setIconSize(QSize(116,68));
pb->setStyleSheet("QPushButton{border:0px; outline: none;}");
pb->show();
}
To copy to clipboard, switch view to plain text mode
This code is working fine in Windows, i.e. the button doesn't get shifted (sunken) when clicked. But in Linux the button is moving/shifting/sinking when clicked.
i found that out by passing same image for normal and pressed image.
In case of Windows, nothing happens when the button is clicked, but in Linux case, click is seen by sunken/risen images.
I want the Windows type behavior, since the pressed image is also passed by me which is already sunken. So basically in Linux case, it gets sunken twice (once by the image, and other by QT native)
I have tried some QProxyStyle
class MyProxyStyle : public QProxyStyle
{
public:
MyProxyStyle
(QStyle *style
= 0) : QProxyStyle
(style
) { } int pixelMetric
(PixelMetric metric,
const QStyleOption *option
= 0,
const QWidget *widget
= 0) { cout<<"pixelMetric enter"<<endl;
int ret = 0;
switch (metric) {
case QStyle::PM_ButtonShiftHorizontal: case QStyle::PM_ButtonShiftVertical: ret = 0;//QProxyStyle::pixelMetric(metric, option, widget)+20;
break;
default:
ret = QProxyStyle::pixelMetric(metric, option, widget);
break;
}
return ret;
}
};
class MyProxyStyle : public QProxyStyle
{
public:
MyProxyStyle(QStyle *style = 0) : QProxyStyle(style) { }
int pixelMetric(PixelMetric metric, const QStyleOption *option = 0, const QWidget *widget = 0) {
cout<<"pixelMetric enter"<<endl;
int ret = 0;
switch (metric) {
case QStyle::PM_ButtonShiftHorizontal:
case QStyle::PM_ButtonShiftVertical:
ret = 0;//QProxyStyle::pixelMetric(metric, option, widget)+20;
break;
default:
ret = QProxyStyle::pixelMetric(metric, option, widget);
break;
}
return ret;
}
};
To copy to clipboard, switch view to plain text mode
And in cpp file, I tried
pb->setStyle(new MyProxyStyle(pb->style()));
pb->setStyle(new MyProxyStyle(pb->style()));
To copy to clipboard, switch view to plain text mode
But this is having no effect. Also the "cout" under pixelMetric is not getting executed. So maybe the style is not getting set to this PushButton.
Can anyone please help me. I am completely stuck on this.
I am using Qt 5.3.1 (also tried on Qt 5.2.1)
Bookmarks