QT下 TableWidget 实现右键菜单栏,以及TableWidget整体美化工作。

来源:互联网 发布:优美句子知乎 编辑:程序博客网 时间:2024/05/01 02:30

转载请注明出处: http://blog.csdn.net/elfprincexu


QT TableWidget 是一个很常用的widget, 可以实现类似excel表格的效果。现在讲解一下如何实现右键菜单项选择。先附图如下




代码当中, cpp 文件当中:_tableWidget->setContextMenuPolicy(Qt::CustomContextMenu);connect (_tableWidget, SIGNAL(customContextMenuRequested(QPoint)), SLOT(customMenuRequested(QPoint)))  // right click connectionvoid MyClass::customMenuRequested(QPoint pos) {QTableWidgetItem* selecteditem = _tableWidget->itemAt(pos) ;//get right click pos item/** do sth  relative with current selected item*  QString finalStr = selecteditem->text();*/QMenu* menu = new QMenu(this);QAction* action = new QAction(tr(&Open Directory), this);action->setData(finalStr );// set data, for later in slot, we can get necessary infomenu->addAction(action);menu->popup(_tableWidget->viewport()->mapToGlobal(pos)) ; //show the menu at right click posconnect (action, SIGNAL(triggered()), this, SLOT(openDirectory));}void myClass::openDirectory(){QAction* action = qobject_cast<QAction*> (sender());if (action == 0) return;QString str = action->data();//get previous data saved by us //do sth relative with the actionQProcess proc;proc.setWorkingDirectory(str);// set current path as str we got proc.start("gnone-Terminal");// open a new terminal bool result = proc.waitForStarted(1000);result& = proc.waitForFinished(1000);if (result) qDebug()<<"success";else qDebug()<<"failure";}头文件中添加一个slot: void customMenuRequested(QPoint pos);void openDirectory();


附上tablewidge常用修饰代码: header 字体颜色,伸缩框样式,整行选取等


    // set header for tablewidget    _tableWidget->setColumnCount(2);    QStringList headers;    headers<<"Creation Time"<<"GenDebug Name" ;    _tableWidget->setHorizontalHeaderLabels(headers);    // only selecting entire rows    _tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows);    // only single row selection    _tableWidget->setSelectionMode(QAbstractItemView::SingleSelection);    // forbidden edit    _tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);    // customcontext menu    _tableWidget->setContextMenuPolicy(Qt::CustomContextMenu);    // set header font, bold    QFont font = _tableWidget->horizontalHeader()->font();    font.setBold(true);    _tableWidget->horizontalHeader()->setFont(font);    _tableWidget->horizontalHeader()->setStretchLastSection(true);    _tableWidget->horizontalHeader()->resizeSection(0,150);    // set selection mode    _tableWidget->setStyleSheet("selection-background-color: darkblue");  // selection background color :blue    // set header height    _tableWidget->horizontalHeader()->setFixedHeight(25);    // set header bg color, horizontalscroll vericalscrollbar style    _tableWidget->horizontalHeader()->setStyleSheet("QHeaderView::section{background:skyblue;}");    _tableWidget->horizontalScrollBar()->setStyleSheet("QScrollBar{background:transparent; 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;}");    _tableWidget->verticalScrollBar()->setStyleSheet("QScrollBar{background:transparent; 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;}");    // no highlight to header    _tableWidget->horizontalHeader()->setHighlightSections(false);











0 0
原创粉丝点击