English 中文(简体)
如何在QTreeWidget Header中添加布顿或其他植被?
原标题:How to Add a Button or other widget in QTreeWidget Header?

Can anyone give me a hint on how to place a button in the header of QTreeWidget A minimal example is more than welcome?

问题回答

对于评论中的人(以及基本上由谁阻止谁帮助的人)来说,下面是《法典》的译本:

from PySide2 import QtWidgets, QtCore

import sys



class Header(QtWidgets.QHeaderView):
    def __init__(self, orientation, parent=None):
        super(Header, self).__init__(orientation, parent)

        self.button = QtWidgets.QPushButton( Button text , self)



class TreeWidget(QtWidgets.QTreeWidget):
    def __init__(self, parent=None):
        super(TreeWidget, self).__init__(parent)

        item0 = QtWidgets.QTreeWidgetItem(["Item 0"])
        item1 = QtWidgets.QTreeWidgetItem(["Item 1"])

        self.addTopLevelItem(item0)
        self.addTopLevelItem(item1)
        self.createHeader()

    def createHeader(self):
        header = Header(QtCore.Qt.Horizontal, self)
        self.setHeader(header)



if __name__ ==  __main__ :
    app = QtWidgets.QApplication(sys.argv)

    w = TreeWidget()
    w.show()
    sys.exit(app.exec_())


#QTreeWidget header is an "stand alone" widget - QHeaderView, so you can set the custom implementation of it.
#include <QTreeWidget>
#include <QTreeWidgetItem>
#include <QHeaderView>
#include <QPushButton>


class Header
    : public QHeaderView
{
public:
    Header(QWidget* parent)
        : QHeaderView(Qt::Horizontal, parent)
        , m_button(new QPushButton("Button", this))
    {
    }

private:
    QPushButton* m_button;
};

class TreeWidget
    : public QTreeWidget
{
    Q_OBJECT
public:
    TreeWidget()
        : QTreeWidget(0)
    {
        QTreeWidgetItem* item0 = new QTreeWidgetItem(QStringList("Item 0"));
        QTreeWidgetItem* item1 = new QTreeWidgetItem(QStringList("Item 1"));
        addTopLevelItem(item0);
        addTopLevelItem(item1);
        createHeader();
    }

private:
    void createHeader()
    {
        m_header = new Header(this);
        setHeader(m_header);
    }

private:
    Header* m_header;

};

int main(int c, char** v)
{
    QApplication a(c, v);

    TreeWidget* tw = new TreeWidget();
    tw->show();

    return a.exec();
}


//QTreeWidget header is an "stand alone" widget - QHeaderView, so you can set the custom implementation of it.




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签