Search code examples
c++qtqt-designer

Adding QWidget into QGridLayout adds border to layout?


Currently I have my program organized this way in QTDesigner (which I use with VS 2022): QMainWindow->centralWidget(QWidget)->QTabWidget->Tab(QWidget)->QGridLayout. All these elements are created in QtDesigner. In my cpp code I'm downloading some data and generating QTableWidget* m_table. Unfortunately after adding it to QGridLayout element, I get a black border exactly around this layout. How it's possible if it border can't be set at all for this element, at least in QtDesigner?

QTableWidget* m_table;
///(...)
ui.gridLayout->addWidget(m_table);

Solution

  • Look into Qt's stylesheets. You should be able to style your QTableWidget in any way you want to.

    Keep in mind a QTableWidget is specialized version of QTableView, so you have set your stylesheet to a QTableView:

    m_table->setStyleSheet("QTableView {background: transparent; border: 1px solid green;}");
    

    Here are some links to get you started with Qt stylesheets:

    https://doc.qt.io/qt-5/stylesheet.html

    https://doc.qt.io/qt-5/stylesheet-syntax.html

    https://doc.qt.io/qt-5/stylesheet-examples.html