0

I am facing issues when I adjust QCheckbox into QTableWidget.

It is working as expected in Mac and Linux but creating problem in Windows.

I have googled it and tried out different solutions, but it did not solve my problem.

Code:

QWidget* cellWidget = new QWidget();
QCheckBox *box = new QCheckBox();
box->setCheckState(Qt::Unchecked);
QHBoxLayout* layout = new QHBoxLayout(cellWidget);
layout->addWidget(box);
layout->setAlignment(Qt::AlignCenter);
layout->setContentsMargins(0, 0, 0, 0);
cellWidget->setLayout(layout);
ui->twidget_header->setCellWidget(0, 0, cellWidget);

Mac OS O/P : As Expected

enter image description here
Win OS O/P : Problem with Checkbox size and alignment

enter image description here .

My app is created in Qt 5.9 for Mac, Win and Linux platform. Let me know if you required more info about the problem.

AB Bolim
  • 1,997
  • 2
  • 23
  • 47

2 Answers2

0

The design and size of the widgets are determined by the style. To override the native style (whatever QApplication sets as default on your target system) you'll have to derive your own QStyle for that target system and and reimplement pixelMetric() to return the appropriate values for QStyle::PM_IndicatorWidth and QStyle::PM_IndicatorHeight (I think; didn't check that).

Murphy
  • 3,827
  • 4
  • 21
  • 35
  • I have try this one but dint solved my problem. I got the solution and posting as an answer so, it will save someone else time in future. – AB Bolim Nov 15 '17 at 06:01
0

The problem can happen with any QWidget while you create Qt app and support different resolutions.

To overcome the size of the QWidget (QCheckbox/QRadiobutton), you just need to overwrite the QWidget class.

Create customcheckbox.h file and overwrite the QCheckbox widget.

class CustomCheckBox : public QCheckBox
{
  Q_OBJECT
public:
    CustomCheckBox(QWidget *parent=NULL);

private:
    bool m_isChecked;

public slots:
    void emitToggleSignal(bool);
};

Implement customcheckbox.cpp file as per your requirement.

By doing this, QCheckbox will automatically adjust size on respective resolution.

Hope this will save someone's time in future.

Thanks.

AB Bolim
  • 1,997
  • 2
  • 23
  • 47