PDA

View Full Version : checkbox implementation in QTableWidget display issue



prajesh
25th February 2010, 13:00
Hello

I have an issue with checkbox implementation in QTableWidget when running on platform other than windows. When running on other platform, the checkbox visibilty is less. The tick color of Qt::checked seems white and its difficult to get to know whether it is ticked or not.

The implementation of Check box is shown below.


QTableWidget* m_GridWgt = new QTableWidget(3,3);
QTableWidgetItem* qItem = new QTableWidgetItem();
qItem->setFlags(Qt::ItemIsEnabled|Qt::ItemIsUserCheckable );
qItem->setCheckState(Qt::Checked);
m_GridWgt->setItem(i, col, qItem);

Is it possible to modify check box color (cell color and tick color of check box) in QTablewidget?

Also to customise the checkbox in a column, I have use another way using delegate item. But I couldnt find a way to set QModelIndex with data Qt::CheckRole.

Is it possible to use QModelIndex with QTableWidget or it works fine only for QTableView?

thx
P

BalaQT
25th February 2010, 13:22
u can use stylesheet for coloring the table



QTableWidget QTableCornerButton::section {
background: red;
border: 2px outset red;
}


m_GridWgt->setStyleSheet("selection-background-color: qlineargradient(x1: 0, y1: 0, x2: 0.5, y2: 0.5,stop: 0 #FF92BB, stop: 1 white)");

this will change the background color when u select a row

i dont know how to change the checkbox;s color and tick color

Bala

BalaQT
25th February 2010, 14:11
hi prajesh

This will change the background color of the checkbox.

m_GridWgt->setStyleSheet("QTableWidget::indicator:checked {background-color:blue;}");

i think. ,the following stylesheet will be handy for ur need
pls prepare the image for checking;

m_GridWgt->setStyleSheet("QTreeView::indicator:checked {image: url(path);}");

hope it helps

Bala

prajesh
25th February 2010, 14:27
Hello Bala,

Yes that works for me and thanks for your reply. But to calrify a doubt, by this method we can set background color for checked or unchecked state ,but I couldnt see tick on the check box if I set style sheet . Is that possible to set color to tick?

rgds
Prajesh

BalaQT
25th February 2010, 14:47
hi prajesh,
Have a image for tick mark with the prefered color
and use the stylesheet
m_GridWgt->setStyleSheet("QTreeView::indicator:checked {image: url(path);}");

so when the checkbox is checked the image will be loaded.
u have to create a image and load it into ur .pro file

hope it helps
Bala

prajesh
26th February 2010, 07:25
Hello Bala

I am sorry to disturb you again.

With image url, Its not at all working for me. Please check the code below to confirm any error made in code


QTableWidget* m_GridWgt = new QTableWidget(3,3);
QTableWidgetItem* qItem = new QTableWidgetItem();
qItem->setFlags(Qt::ItemIsEnabled|Qt::ItemIsUserCheckable );
qItem->setCheckState(Qt::Checked);
m_GridWgt ->setStyleSheet("QTableWidget::indicator:checked {image: url(D:\checkbox.png);}");
m_GridWgt->setItem(i, col, qItem);

Setting background color is working fine for me . m_GridWgt ->setStyleSheet("QTableWidget::indicator:checked {background-color:blue;}");

To confirm I have tried with below code


QTableWidget* m_GridWgt = new QTableWidget(3,3);
QCheckBox* qItem = new QCheckBox();
qItem->setCheckState(Qt::Checked);
m_GridWgt ->setStyleSheet("QCheckBox::indicator:checked {image: url(D:\checkbox.png);}");
m_GridWgt->setItem(i, col, qItem);

This is also not working for me.
To double check the problem is with image, I load image image with another QT application and is getting fine.

Do you see any wierdness in code? Is this has effect only with QTableview?

I am attaching the image I used ,
thx
Prajesh

BalaQT
26th February 2010, 08:30
hi prajesh,

Try this

m_GridWgt->setStyleSheet("QTableWidget::indicator:checked {background-image:url(:/check-box.png);}");
this will surely work.
i hav tested it out.
pls resize ur image to there.

steps:
1) add the image in qrc file [do u know how to add to the resource file?]
2) then apply the stylesheet
dont forget to resize the image to fit within checkbox

hope it helps
Bala

BalaQT
26th February 2010, 08:40
m_GridWgt ->setStyleSheet("QTableWidget::indicator:checked {image: url(D:\checkbox.png);}");
pls add the imagefile to the resource file

To add a image to a resource file
steps :
right click on ur project . click add new.
then select Qt Resource file
then give a name
press ok.
then click add ->addprefix
let it be default.
and again click add-> addfiles
select ur file
remove the prefix text."/new/prefix1"

ur done.

then use the stylesheet.

i hav attached the checkbox image to fit within table's checkbox;
pls refer the attachments

or resize ur old image to fit.

hope it helps
Bala

BalaQT
26th February 2010, 08:52
The image should be on ur project folder [copy the checkbox image to ur projects folder]

have u tried?
Bala

prajesh
26th February 2010, 09:12
Hello Bala,

As you siad , I resize the image and added as resource file. But unfortunately I couldnt work fine. Do you see the same check box in the image or windows by default check box. I pointed out this because, in windows checkbox is visible even with out set style sheet. The problem is when we run on WCE target.

In Windows Do you see the same checkbox with tick color green?

Is it possible to share your code?

thx
Prajesh

BalaQT
26th February 2010, 09:30
In Windows Do you see the same checkbox with tick color green?
yeah its working
i hav attached here

Bala

prajesh
26th February 2010, 10:35
Hello Bala,

Thanks a lot for your reply. With your code and settings it is working fine for me. I am using VS and may be the problem with mocing files. I will check .

Thanks a lot
Prajesh