QTableWidget设置网格线粗细 单元格中添加控件并居中

来源:互联网 发布:foxpro是什么软件 编辑:程序博客网 时间:2024/05/16 09:31

通过网上搜集资料整理,方便自己和他人以后查阅

tableWidget = newQTableWidget(3,2);

    
//http://zhidao.baidu.com/link?url=GAP652gyVHuLThmigsYh1kVYMI-kAiaKPHayyZmd45DNUfqhOO8ULGgVE4QmYoTEqpHe4eyltfoyadvQA5TP8K
tableWidget->setStyleSheet("QTableWidget::item{border:1px solid ;}");
 
//
    //表格表头的显示与隐藏
    tableWidget->verticalHeader()->setVisible(false);  //隐藏列表头
    tableWidget->horizontalHeader()->setVisible(false); //隐藏行表头
    //tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
    //设置行高
    for(int i= 0; i< tableWidget->rowCount(); i++)
    {
        tableWidget->setRowHeight(i, 60);
    }
    //设置列宽
    for(int i= 0; i< tableWidget->columnCount(); i++)
    {
        tableWidget->setColumnWidth(i, 185);
    }
    // 单元格中添加控件并居中
     QLabel* label = new QLabel("gender");
     QComboBox *comBox = new QComboBox();
     comBox->setFixedSize(100, 25);
     comBox->addItem("F");
     comBox->addItem("M");
     // 单元格中的控件需要通过布局管理
     QWidget *widget = new QWidget;
     QHBoxLayout *hLayout;
     hLayout = new QHBoxLayout();
     hLayout->addWidget( label);
     hLayout->addWidget(comBox);
     hLayout->setMargin(0);
     hLayout->setAlignment(widget, Qt::AlignCenter);
     hLayout->setContentsMargins(10, 0, 20, 0);
     widget->setLayout(hLayout);
     // 添加单元格
     tableWidget->setCellWidget(0,0,widget);
     QHBoxLayout* mainLayout = new QHBoxLayout;
     mainLayout->addWidget( tableWidget);
     setLayout(mainLayout);

 

原创粉丝点击