PDA

View Full Version : Align a QPushButton in the cellwidget of QTableWidget



rawfool
2nd May 2012, 12:54
I've created a custom class(DeviceState_Indicator) which inherits QPushButton. I want to use that as an LED indicator and I'm successful in doing that. Now I've placed that widget in a cell of a QTableWidget.



for(int iCount = 0; iCount < NUM_OF_DEVICES; iCount++)
{
oDeviceStateIndication[iCount] = new DeviceState_Indicator(0, INDICATOR_GREEN);
oDevStatePanel->setCellWidget(iCount, 2, oDeviceStateIndication[iCount]);
}

The problem is, I'm unable to align the DeviceState_Indicator widget to the center (as shown in the pic attached). How do I align it to center?

Lykurg
2nd May 2012, 15:05
As fas as I know, the widget takes the whole space of the cell, so the custom widget has to paint itself centered. Or you insert QWidget which has a layout, spacers and the LED-Widget. But this would be madness. Alter your LED class that is paints the "image" centered.

rawfool
3rd May 2012, 07:42
Alter your LED class that is paints the "image" centered.

It's not an image, it's a QPushButton. I'm not getting anything that gives me options to align/space/set margin.


Is there any widget or something (which is not an image) which I can use as an LED indicator inside a QTableWidget cell?



#ifndef DEVICESTATE_INDICATOR_H
#define DEVICESTATE_INDICATOR_H

#include <QPushButton>
#include <QString>

#define INDICATOR_GREEN "background-color: green; border-radius: 7px; "
#define INDICATOR_RED "background-color: red; border-radius: 7px; "
#define INDICATOR_YELLOW "background-color: yellow; border-radius: 7px; "
#define INDICATOR_GRAY "background-color: lightGray; border-radius: 7px; "


class DeviceState_Indicator : public QPushButton
{
Q_OBJECT
public:
explicit DeviceState_Indicator(QWidget *parent, QString Indicator_Color);

signals:

public slots:

};

#endif // DEVICESTATE_INDICATOR_H


Thank you.

Spitfire
9th May 2012, 09:55
Question is, what for you need it to be a button?

Simple row delegate will solve all your issues with painting and combined with cellActivated() signal it will act as button.
What else do you need?