setSelectionBehavior(QAbstractItemView::SelectRows);//设置选择整行,而不是单个Item
setSelectionMode(QAbstractItemView::SingleSelection);//设置只能选择一行,不能按住ctrl或者shift多选
a.setStyleSheet("QTableView{alternate-background-color:rgb(0,255,0,35);}QTableView::item{selection-background-color:rgb(0,255,0);}");
//如果行的背景色需要交替需要设置void setAlternatingRowColors ( bool enable )为true,然后在用样式表QTableView{alternate-background-color:rgb(0,255,0,35)见上面的,如果不需要交替就QTableView::item{selection-background-color:rgb(0,255,0);}这样就可以了,用法见上面。QTableWidget应该也是一样的。我用QTableView试的。