PDA

View Full Version : Changing style of Button,Label...



Anshuman
25th April 2011, 07:36
hello friends

Actually i want to set the background image of an button using codding,not in the qtDesigner Property part..so hw can i do it..moreover hw can i change the font color,font style of an button,label,...

with regards
Anshuman

Lykurg
25th April 2011, 08:02
Use the palette to change things: QPalette. Or create your own style: QStyle.

SixDegrees
25th April 2011, 08:07
Also, be prepared for bitter disappointment when your code fails to run on some platforms (Windows) while running perfectly elsewhere. For example, it is impossible to do something as simple as setting the background color of a button programatically when running under Windows.

Unless you use stylesheets. But once you open that door, you're responsible for defining the entire style of the button - border, margin, corner rounding, shading, font color, background color....

Anshuman
25th April 2011, 08:40
hello lykurg

Actually i had use QStyle but its nt working..i will send u the code..u just check it


QString style="QPushButton::forwardBut { width:26px; height:17px;"
"image: url(icons:right_arrow.png); } ";

QPushButton *forwardBut=new QPushButton("->");
forwardBut->setStyleSheet(style);

with regards
Anshuman

Anshuman
25th April 2011, 10:44
hello sir

As per ur suggestion i will be able to create an array of buttons consisting of six rows and seven colums....which look like that.

<- April -> // Arow sign are in button..representing forward and backward to change month

M T W Th F S SU //weeks are in label

1 2 3 4 5 6 7

8 9 10 11 12 13 14

.................................................. .......

.......................42

Number in bold are buttons stored in an array of a[6][7]...

now i am trying to update each button on changing of month...as per calender work..please help me to suggest that hw can i implement this to work it as calender...as far of my code that i had wrritten is given below..from there hw can i proceed please suggest...


MainWindow::MainWindow(QWidget *parent) :QMainWindow(parent),ui(new Ui::MainWindow)
{

ui->setupUi(this);

QWidget *centralWidget = new QWidget;
selectedDate=QDate::currentDate();
x=selectedDate.month();
// QString style="QPushButton::forwardBut { width:26px; height:17px;"
// "image: url(icons:right_arrow.png); } ";

int count=1,i,j;
MonthLbl=new QLabel("MonthDisplay");
MonthLbl->setText(selectedDate.longMonthName(x));;
QPushButton *button[10][10],*forwardBut,*backwardBut;

forwardBut=new QPushButton();

forwardBut->setIcon(QIcon("C:/Plackal/QtWorkspace/CustomCalender/Images/back-icon.PNG"));

backwardBut=new QPushButton("<-");
QGridLayout *controlsLayout = new QGridLayout;
QGridLayout *hWeek = new QGridLayout;

QVBoxLayout *v=new QVBoxLayout;
QHBoxLayout *h=new QHBoxLayout;
QHBoxLayout *hLbl=new QHBoxLayout;
QLabel *blankLbl=new QLabel;

QLabel *blank=new QLabel;

QLabel *weekLbl[7];
for(i=1;i<=7;i++)
{
weekLbl[i]=new QLabel(selectedDate.shortDayName(i));
hWeek->addWidget(weekLbl[i],1,i);
}

QTextBrowser *txtDate=new QTextBrowser;

for(i=0;i<7;i++)
{
for(j=0;j<7;j++)
{
if(count<=42)
{
button[i][j] = new QPushButton(QString::number(count));
controlsLayout->addWidget(button[i][j], i, j);
count++;
}
}
}

controlsLayout->setMargin(0);
controlsLayout->setHorizontalSpacing(0);
controlsLayout->setVerticalSpacing(0);
h->addWidget(backwardBut);
h->addWidget(blank);
h->addWidget(MonthLbl);
h->addWidget(blank);
h->addWidget(forwardBut);
hLbl->addWidget(blankLbl);
v->addLayout(h);
v->addLayout(hLbl);
v->addLayout(hWeek);
v->addLayout(controlsLayout);
v->addWidget(txtDate);
//centralWidget->setLayout(controlsLayout);
centralWidget->setLayout(v);
setCentralWidget(centralWidget);
connect(forwardBut,SIGNAL(clicked()),this,SLOT(mon thForward()));
connect(backwardBut,SIGNAL(clicked()),this,SLOT(mo nthBackward()));
}
void MainWindow::monthForward()
{
MonthLbl->setText(selectedDate.longMonthName(++x));
if(x>12)
x=1;
}
void MainWindow::monthBackward()
{
MonthLbl->setText(selectedDate.longMonthName(--x));
if(x<2)
x=13;
}
with regards
Anshuman