QTableWidget 使用及美化_QtableWidget_QtableView滾動條寬度及樣式

 
//建立及屬性設置 m_tableWidget = new QTableWidget(this); m_tableWidget->setRowCount(10); m_tableWidget->setColumnCount(3); //m_tableWidget->setFrameShape(QFrame::NoFrame); //設置邊框 //m_tableWidget->setShowGrid(false); //設置不顯示格子線 //QPalette pll = m_tableWidget->palette(); //pll.setBrush(QPalette::Base,QBrush(QColor(255,255,255,0))); //m_tableWidget->setPalette(pll); //m_tableWidget->horizontalHeader()->setStyleSheet("QHeaderView::section {background-color:lightblue;color: black;padding-left: 4px;border: 1px solid #6c6c6c;}"); m_tableWidget->horizontalHeader()->setDefaultSectionSize(150); m_tableWidget->horizontalHeader()->setClickable(false); //設置表頭不可點擊(默認點擊後進行排序) //設置表頭內容 QStringList header; header<<tr("Index")<<tr("Name")<<tr("Time"); m_tableWidget->setHorizontalHeaderLabels(header); //設置表頭字體加粗 QFont font = m_tableWidget->horizontalHeader()->font(); font.setBold(true); m_tableWidget->horizontalHeader()->setFont(font); m_tableWidget->horizontalHeader()->setStretchLastSection(true); //設置充滿表寬度 m_tableWidget->verticalHeader()->setResizeMode(QHeaderView::ResizeToContents); m_tableWidget->verticalHeader()->setDefaultSectionSize(10); //設置行距 m_tableWidget->setFrameShape(QFrame::NoFrame); //設置無邊框 m_tableWidget->setShowGrid(false); //設置不顯示格子線 m_tableWidget->verticalHeader()->setVisible(false); //設置垂直頭不可見 m_tableWidget->setSelectionMode(QAbstractItemView::ExtendedSelection); //可多選(Ctrl、Shift、 Ctrl+A均可以) m_tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows); //設置選擇行爲時每次選擇一行 m_tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers); //設置不可編輯 m_tableWidget->horizontalHeader()->resizeSection(0,20); //設置表頭第一列的寬度爲150 m_tableWidget->horizontalHeader()->resizeSection(2,50); //設置表頭第一列的寬度爲150 m_tableWidget->horizontalHeader()->setFixedHeight(25); //設置表頭的高度 //m_tableWidget->setStyleSheet("background: rgb(56,56,56);alternate-background-color:rgb(48,51,55);selection-background-color:qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 rgb(56,56,56),stop:1 rgb(76,76,76));"); //設置選中背景色 //m_tableWidget->horizontalHeader()->setStyleSheet("QHeaderView::section{background-color: qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 rgb(46,46,46),stop:1 rgb(66,66,66));color: rgb(210,210,210);;padding-left: 4px;border: 1px solid #383838;}"); //設置表頭背景色 m_tableWidget->setAlternatingRowColors(true); m_tableWidget->setFocusPolicy(Qt::NoFocus); //去除選中虛線框 //設置水平、垂直滾動條樣式 //m_tableWidget->horizontalScrollBar()->setStyleSheet("QScrollBar{background:red; height:10px;}" // "QScrollBar::handle{background:lightgray; border:2px solid transparent; border-radius:5px;}" // "QScrollBar::handle:hover{background:gray;}" // "QScrollBar::sub-line{background:transparent;}" // "QScrollBar::add-line{background:transparent;}"); //m_tableWidget->verticalScrollBar()->setStyleSheet("QScrollBar{background:red; width: 10px;}" // "QScrollBar::handle{background:lightgray; border:2px solid transparent; border-radius:5px;}" // "QScrollBar::handle:hover{background:gray;}" // "QScrollBar::sub-line{background:transparent;}" // "QScrollBar::add-line{background:transparent;}"); /*QPalette pal; pal.setColor(QPalette::Base, QColor(255, 0, 0)); pal.setColor(QPalette::AlternateBase, QColor(0, 255, 0)); m_tableWidget->setPalette(pal);*/ m_tableWidget->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); m_tableWidget->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); m_tableWidget->setAlternatingRowColors(true); m_tableWidget->setShowGrid(false);
相關文章
相關標籤/搜索