PDA

View Full Version : Image on button



windkracht8
11th August 2006, 13:52
Hello all,

some help please with this puzzle:

I'm trying to put an image on a pushbutton. I wrote this program in Qt3 on Linux, later I rewrote it to Qt4. So no Qt3 support or anything, only Qt4 left.
Now I'm trying to compile it on Windows. But I cannot get the image to show on the pushbutton on Windows. Works on Linux.

This is what I'm trying to do: Can somebody see the problem with this?



from knop.h:
QPixmap pWit;
QPalette palWit;
QString progPath;

from knop.c:

knop::knop(QWidget* parent)
: QWidget(parent)
{
setMaximumSize( QSize( 50, 50 ) );

but = new QPushButton("but", this);
but->setGeometry( QRect( 0, 0, 50, 50 ) );
but->setCheckable(true);
but->setFont(QFont("Bitstream Vera Sans",22,QFont::Bold));
but->setFlat(true);

e = new QLineEdit("e", this);
e->setGeometry( QRect( 5, 5, 40, 10 ) );
QFont e_font( e->font() );
e_font.setPointSize( 10 );
e->setFont( e_font );
e->setFrame( FALSE );

resize( QSize(50, 50).expandedTo(minimumSizeHint()) );
connect( but, SIGNAL(clicked()), SLOT(butClick()) );
init();

}

void knop::init()
{
#ifdef _WIN32
progPath = "/home/bartv/progs/sudoku/"; //linux
#else
progPath = "D:/qt/progs/sudoku/"; //windows
#endif

pWit.load("knopWit.png");

palWit.setBrush(QPalette::Active,QPalette::Window, QBrush(pWit));
palWit.setBrush(QPalette::Active,QPalette::Base,QC olor(255,255,255));
palWit.setBrush(QPalette::Active,QPalette::Text,QC olor(0,0,0));
palWit.setBrush(QPalette::Active,QPalette::ButtonT ext,QColor(0,0,0));
palWit.setBrush(QPalette::Inactive,QPalette::Windo w,QBrush(pWit));
palWit.setBrush(QPalette::Inactive,QPalette::Base, QColor(255,255,255));
palWit.setBrush(QPalette::Inactive,QPalette::Text, QColor(0,0,0));
palWit.setBrush(QPalette::Inactive,QPalette::Butto nText,QColor(0,0,0));

but->setPallete(palWit);
e->setPalette(palWit);

jacek
11th August 2006, 14:12
Where do you use progPath?

PS. You might find QCoreApplication::applicationDirPath() useful.

windkracht8
11th August 2006, 18:59
Thanks Jacek,

but not the answer I was looking for at te moment.

The solution to the problem at hand: put an label behind the button.
In Qt4 it's not posible anymore to put an image on an button. Only labels.
So label behind button, alpha of the button to 0.



label = new QLabel("label", this);
label->setGeometry( QRect( 0, 0, 50, 50 ) );
label->setText("");

but = new QPushButton("but", this);
but->setGeometry( QRect( 0, 0, 50, 50 ) );
but->setCheckable(true);
but->setFont(QFont("Bitstream Vera Sans",22,QFont::Bold));
but->setFlat(true);

e = new QLineEdit("e", this);
e->setGeometry( QRect( 5, 5, 40, 10 ) );
QFont e_font( e->font() );
e_font.setPointSize( 10 );
e->setFont( e_font );
e->setFrame( FALSE );

resize( QSize(50, 50).expandedTo(minimumSizeHint()) );
connect( but, SIGNAL(clicked()), SLOT(butClick()) );
init();

}

knop::~knop(){}

void knop::init()
{
pWit.load("/home/bartv/progs/sudoku/include/knopWit.png");
pRood.load("/home/bartv/progs/sudoku/include/knopRood.png");
pGroen.load("/home/bartv/progs/sudoku/include/knopGroen.png");


palWit.setColor(QPalette::Base,QColor(255,255,255) );
palWit.setColor(QPalette::Button,QColor(255,255,25 5,0));

palRood.setColor(QPalette::Base,QColor(255,0,0));

palGroen.setColor(QPalette::Base,QColor(0,255,0));

palClick.setColor(QPalette::Base,QColor(150,150,15 0));
palClick.setColor(QPalette::Button,QColor(150,150, 150,255));

label->setPixmap(pWit);
but->setPalette(palWit);
e->setPalette(palWit);


Cheers,
Bart.

windkracht8
11th August 2006, 19:07
ps.

-cut- from "QCoreApplication::applicationDirPath()"
Warning: On Unix, this function assumes that argv[0] contains the file name of the executable (which it normally does). It also assumes that the current directory hasn't been changed by the application.

Sounds like it's the same as "QDir::Path()".

Cheers,
Bart.

Brandybuck
11th August 2006, 19:08
In Qt4 it's not posible anymore to put an image on an button.
Yet you say you can do it in Linux!!


pWit.load("/home/bartv/progs/sudoku/include/knopWit.png");
pRood.load("/home/bartv/progs/sudoku/include/knopRood.png");
pGroen.load("/home/bartv/progs/sudoku/include/knopGroen.png");

Do these paths exist under Windows?

jacek
11th August 2006, 19:17
Sounds like it's the same as "QDir::Path()".
Yes, but it's a static method.

jacek
11th August 2006, 19:21
In Qt4 it's not posible anymore to put an image on an button.
It is possible as it works for me (on both Linux and windows) --- you just have to use correct paths.

windkracht8
14th August 2006, 07:38
It is possible as it works for me (on both Linux and windows) --- you just have to use correct paths.

Could you please post your code on howto do this here please.

To clear up things:
This is in init: (Could have been done with QDir::progPath(), but not implemented yet)


#ifdef _WIN32
progPath = "D:/qt/progs/sudoku/"; //windows
#else
progPath = "/home/bartv/progs/sudoku/"; //linux
#endif

pWit.load("knopWit.png");

palWit.setColor(QPalette::Base,QColor(255,255,255) );
palWit.setColor(QPalette::Button,QColor(255,255,25 5,0));
palWit.setColor(QPalette::Text,QColor(0,0,0,255));
palWit.setColor(QPalette::ButtonText,QColor(0,0,0, 255));

label->setPixmap(pWit);
but->setPalette(palWit);
e->setPalette(palWit);



@Brandybuck:
With Qt3.3, not with Qt4 anymore, sorry for the mixup.

Cheers,
Bart.

jacek
14th August 2006, 09:37
Could you please post your code on howto do this here please.
Try something like:
pWit.load( QCoreApplication::applicationDirPath() + QDir::separator() + "knopWit.png" );
but->setIcon( pWit );

windkracht8
14th August 2006, 11:21
That give's me a small icon on the button. I need the entire image to be the entire button.
So people see my image not the button.

Can't put an image on the net at the moment, no ftp allowd here.

But I got it to work. It's only a shame I need to put an label behind the button.

Cheers,
Bart.

jacek
14th August 2006, 11:38
That give's me a small icon on the button. I need the entire image to be the entire button. So people see my image not the button.
Maybe QPushButton::setFlat() will be enough?


Can't put an image on the net at the moment, no ftp allowd here.
Just post it as attachment (click "Manage Attachments" below the editor).

windkracht8
14th August 2006, 12:10
All ready got that set.

knop.cpp is an custom widget which is placed a couple of times in an frame on the main form.
Complete code included. Maybe someone is interrested in a qt cross-platform sudoku game. It generates games on different levels.

Now I just need to get it up and running on my phone. (windows mobile) :)

knop.cpp:


#include "knop.h"
#include "bc.h"

#include <QVariant>
#include <QLineEdit>
#include <QPushButton>
#include <QString>
#include <QApplication>
#include <QMessageBox>
#include <QTimer>

knop::knop(QWidget* parent)
: QWidget(parent)
{
setMaximumSize( QSize( 50, 50 ) );

label = new QLabel("label", this);
label->setGeometry( QRect( 0, 0, 50, 50 ) );
label->setText("");

but = new QPushButton("but", this);
but->setGeometry( QRect( 0, 0, 50, 50 ) );
but->setCheckable(true);
but->setFont(QFont("Bitstream Vera Sans",22,QFont::Bold));
but->setFlat(true);

e = new QLineEdit("e", this);
e->setGeometry( QRect( 5, 5, 40, 10 ) );
QFont e_font( e->font() );
e_font.setPointSize( 10 );
e->setFont( e_font );
e->setFrame( FALSE );

resize( QSize(50, 50).expandedTo(minimumSizeHint()) );
connect( but, SIGNAL(clicked()), SLOT(butClick()) );
init();

}

knop::~knop(){}

void knop::init()
{
progPath = QCoreApplication::applicationDirPath() + "/";

pWit.load(progPath + "include/knopWit.png");
pRood.load(progPath + "include/knopRood.png");
pGroen.load(progPath + "include/knopGroen.png");

palWit.setColor(QPalette::Base,QColor(255,255,255) );
palWit.setColor(QPalette::Button,QColor(255,255,25 5,0));
palWit.setColor(QPalette::Text,QColor(0,0,0,255));
palWit.setColor(QPalette::ButtonText,QColor(0,0,0, 255));

palRood.setColor(QPalette::Base,QColor(255,0,0));
palRood.setColor(QPalette::Text,QColor(0,0,0,255)) ;
palRood.setColor(QPalette::ButtonText,QColor(0,0,0 ,255));

palGroen.setColor(QPalette::Base,QColor(0,255,0));
palGroen.setColor(QPalette::Text,QColor(0,0,0,255) );
palGroen.setColor(QPalette::ButtonText,QColor(0,0, 0,255));

palClick.setColor(QPalette::Base,QColor(200,200,20 0));
palClick.setColor(QPalette::Button,QColor(200,200, 200,255));
palClick.setColor(QPalette::Text,QColor(0,0,0,255) );
palClick.setColor(QPalette::ButtonText,QColor(0,0, 0,255));

label->setPixmap(pWit);
but->setPalette(palWit);
e->setPalette(palWit);

}
void knop::setNum(QString num)
{
but->setText(num);
current = num;
e->setText("");
}

void knop::butClick()
{
bC* bC1 = new bC(knopRij,knopCol);
QApplication::postEvent(this->parent()->parent(), bC1);
}
void knop::unClick()
{
but->setChecked(false);
but->setPalette(palWit);
e->setPalette(palWit);
}
void knop::hint(QString hoort)
{
QTimer::singleShot(2000, this, SLOT(terug()));
but->setText(hoort);
but->setPalette(palGroen);
e->setPalette(palGroen);
}
void knop::terug()
{
but->setText(current);
but->setPalette(palWit);
e->setPalette(palWit);
}
void knop::setRood()
{
but->setPalette(palRood);
e->setPalette(palRood);
QTimer::singleShot(5000, this, SLOT(terug()));
}
void knop::setGroen()
{
but->setPalette(palGroen);
e->setPalette(palGroen);
QTimer::singleShot(5000, this, SLOT(terug()));
}
void knop::clearEdit()
{
e->setText("");
}