如何在QTableWidget表头添加复选框

如何在QTableWidget表头添加复选框,第1张

表双击:

QTableWidget类信号:

void QTableWidget::itemDoubleClicked ( QTableWidgetItem * item ) [signal]

使用信号判断用户双击哪格再通QTableWidgetItem

int column()int row()判断用户双击格(表格)

表格线显示:

使用试试void setShowGrid ( false )

winform datagridview表头怎么增加复选框,点击表头的复选框,下面行的复选全选或全未选

using System

using System.Collections.Generic

using System.ComponentModel

using System.Data

using System.Drawing

using System.Linq

using System.Text

using System.Windows.Forms

namespace WindowsFormsApplication1

{

publicpartialclass Form1 : Form

{

private DataGridView DataGridView1 =new DataGridView()

private CheckBox CheckBox1 =new CheckBox()

public Form1()

{

InitializeComponent()

}

privatevoid Form1_Load(object sender, EventArgs e)

{

CheckBox1.CheckedChanged += CheckBox1_CheckedChanged

DataGridView1.CellPainting += DataGridView1_CellPainting

this.DataGridView1.AllowUserToResizeRows =false

this.DataGridView1.AllowUserToResizeColumns =false

this.DataGridView1.Dock = DockStyle.Fill

this.DataGridView1.Columns.Add(new DataGridViewCheckBoxColumn())

this.DataGridView1.Columns.Add("Column2", "Column2")

for (int i =1i <=3i++)

{

this.DataGridView1.Rows.Add(0, "Row"+ i.ToString() +" Column2")

}

this.CheckBox1.Visible =false

this.CheckBox1.Text ="CheckBox"

this.Controls.Add(DataGridView1)

this.Controls.Add(CheckBox1)

}

privatevoid CheckBox1_CheckedChanged(object send, System.EventArgs e)

{

for (int i =0i <=this.DataGridView1.RowCount -1i++)

{

this.DataGridView1.Rows.SharedRow(i).SetValues(CheckBox1.Checked)

}

}

privatevoid DataGridView1_CellPainting(object sender, System.Windows.Forms.DataGridViewCellPaintingEventArgs e)

{

if (e.RowIndex ==-1&e.ColumnIndex ==0)

{

Point p =this.DataGridView1.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, true).Location

p.Offset(this.DataGridView1.Left, this.DataGridView1.Top)

this.CheckBox1.Location = p

this.CheckBox1.Size =this.DataGridView1.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, false).Size

this.CheckBox1.Visible =true

this.CheckBox1.BringToFront()

}

}

}

}

在 上一篇 中表格已经变得工整了不少,不过看上去还是太素了。在这个看脸的时代这是不行的。那怎么办呢?别担心,Qt最擅长就是干这个了。接下来就用Qt的样式表给表格上点颜色

我们从表头开始,先给弄它弄个背景色,另外字体加粗以示区别。

Qt使用setStyleSheet函数来设置部件的样式表。这里QHeaderView表示表头,section表示表头上层的可选中的区域,由于设置了最后一行拉伸,所以是看不到section下一层的界面。

现在看来表头是凸起的,而且也不够高,接着在设置。

这一步加了三个属性及对应的值,第一个border:none去掉了section的边框。第二个height:35px,设置高度为35像素。顺便设置了第三个color:white设置文字的颜色为白色。 这样看上去舒服多了。接下来给表格也设置一些颜色:

这一步给表格设置了三个属性及对应的值,第一个是gridline-color:#2aaee4,设置网格线的颜色,#2aaee4是用十六进制表示颜色的方法。第二个是color:#888,设置文字的颜色,#888相当于#888888。最后一个是border:none去掉了表格边框,这样看起来更清爽一点了。

如果表格中的行数非常多的话,经常会看走眼了。所以我们要设置间隔行颜色不同以示区分。对于表格需要先开启间隔行背景色的功能,默认间隔行背景色是灰色的,我们可以在样式表中控制这个颜色。

在样式表中使用alternate-background-color属性来控制间隔行的颜色。设置的是偶数行的颜色。如果想设置奇数行的颜色可以通过设置表格的背景颜色来实现。


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/bake/8005467.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-12
下一篇2023-04-12

发表评论

登录后才能评论

评论列表(0条)

    保存