PDA

View Full Version : Qicon



shihao
14th March 2010, 09:33
Hi guys,

I am new to QT creater. What I try to do is, try to
create a button which contains picture.

QPushButton *hello=new QPushButto('button',this);
hello->setIcon(QIcon(/location/pic/));

What should I do to change the size of the icon?

toutarrive
14th March 2010, 09:53
Create a pixmap at the relevant size and then use the constructor:
QIcon::QIcon ( const QPixmap & pixmap )

Lykurg
14th March 2010, 10:11
Use QAbstractButton::setIconSize().

daiheitan
14th March 2010, 14:32
You can also use style sheet.
with the qProperty-iconSize target

shihao
14th March 2010, 17:06
Guys, Thanks for helping. I am totally new on this, can u guys be more specific.?Thank you. My program is almost like this:

class MyWidget : public QWidget
{
public:
MyWidget(QWidget *parent = 0);
};

MyWidget::MyWidget(QWidget *parent)
: QWidget(parent)
{
setFixedSize(900, 400);
QPushButton *hello6 = new QPushButton(tr("hello6"), this);
hello6->setGeometry(460, 200, 75, 50);
hello6->setIcon(QIcon("/shihao/shihao2/exit.JPG"));
}


int main(int argc, char *argv[])
{
QApplication app(argc, argv);
MyWidget widget;
widget.show();
return app.exec();
}

shihao
14th March 2010, 17:09
I wonder which way is the most convinience ways for my program. Thank you.

Lykurg
14th March 2010, 17:19
I would recommend using layouts instead of positioning your items by hand. For the icon just use
hello6->setIconSize(QSize(64,64));

shihao
15th March 2010, 04:03
Guys, my code generate the following output:
button with :cool: hello6

How to generate the output which is
:cool:
Hello6

which is hello6 word at the bottom of the qicon?

Thanks and Regards
Shi Hao

toutarrive
15th March 2010, 08:34
Consider using a QToolButton instead:


QToolButton*mybutton = new QToolButton(this);
mybutton->setIcon(myIcon);
mybutton->setText("Sample text");
mybutton->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);


If it's not an option you can customize your pushButton using style sheet.
http://doc.trolltech.com/4.2/stylesheet.html#customizing-a-qpushbutton-using-the-box-model

shihao
15th March 2010, 10:07
Guys, this forum are helpful compare to the others. Thank you very much to hose who participate.
Anyway, Now I try to do an event, when you click on the menu, it will pop up another sub menu. Anybody know how to do it?

However, I feel embrace to ask every single steps, anyone know any links which is sufficient for beginner?
I have some links as well, like:
http://doc.trolltech.com/4.3/tutorial.html
http://digg.com/programming/Free_e_book_C_GUI_Programming_with_Qt_4_Zip_with_P DF

Lykurg
15th March 2010, 10:33
QToolButton::setMenu() and a good start is the documentation which is really good. If you have found QToolButton just read through its functions.

shihao
15th March 2010, 14:23
hi Guys, my code is something like this,

#include <QApplication>
#include <QFont>
#include <QPushButton>
#include <QVBoxLayout>
#include <QWidget>
#include <QIcon>
#include <qpixmap.h>
#include <QAbstractButton>
#include <QToolButton>
#include <QStyle>
#include <Qt>

class MyWidget : public QWidget
{
public:
MyWidget(QWidget *parent = 0);
};

MyWidget::MyWidget(QWidget *parent)
: QWidget(parent)
{
setFixedSize(900, 400);

QToolButton * mybutton = new QToolButton(this);
mybutton->setIcon(QIcon("/shihao/icon/icon/Technorati.JPG"));
mybutton->setText("Sample text");
mybutton->setToolButtonStyle(Qt::ToolButtonIconOnly);
mybutton->setIconSize(QSize(50,70));
mybutton->setGeometry(270, 70, 95, 100);

QToolButton * mybutton1 = new QToolButton(this);
mybutton1->setIcon(QIcon("/shihao/qicon/qicon7.JPG"));
mybutton1->setText("Sample text");
mybutton1->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
mybutton1->setIconSize(QSize(50,70));
mybutton1->setGeometry(380, 70, 95, 100);
}

int main(int argc, char *argv[])
{
QApplication app(argc, argv);
MyWidget widget;
widget.show();
return app.exec();
}


Apparently, the icon of first image without the text can be align properly. On the other hand, the second image looks a bit bigger in the bottom-padding. (Not same with it's top padding)
What can I do to align the second images exactly on the center?

Thanks in advance.