简体   繁体   中英

Adjust QCheckbox into QTableWidget : Qt

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

在此处输入图片说明
Win OS O/P : Problem with Checkbox size and alignment

在此处输入图片说明 .

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.

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).

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM