文章目录
添加控件,添加行列数 widget.cpp widget.h 效果
添加控件,添加行列数
widget.cpp
# include "widget.h"
# include "ui_widget.h"
Widget :: Widget ( QWidget * parent) :
QWidget ( parent) ,
ui ( new Ui:: Widget)
{
ui-> setupUi ( this ) ;
Init ( ) ;
}
Widget :: ~ Widget ( )
{
delete ui;
}
void Widget :: Init ( )
{
int i, j;
ui-> tableWidget-> horizontalHeader ( ) -> setStretchLastSection ( true ) ;
ui-> tableWidget-> horizontalHeader ( ) -> setSectionResizeMode ( QHeaderView:: ResizeToContents) ;
ui-> tableWidget-> horizontalHeader ( ) -> setSectionResizeMode ( QHeaderView:: Stretch) ;
row_count = ui-> tableWidget-> rowCount ( ) ;
column_count = ui-> tableWidget-> columnCount ( ) ;
qDebug ( ) << "row" << row_count<< "column" << column_count;
for ( i= 0 ; i< row_count; i++ )
{
for ( j= 0 ; j< column_count; j++ )
{
ui-> tableWidget-> setItem ( i, j, new QTableWidgetItem ( QString :: number ( j) ) ) ;
}
}
QTableWidgetItem* item;
QString str;
for ( int i = 0 ; i < ui-> tableWidget-> rowCount ( ) ; i++ ) {
for ( int j = 0 ; j < ui-> tableWidget-> columnCount ( ) ; j++ ) {
item = ui-> tableWidget-> item ( i, j) ;
str = str + item-> text ( ) + " " ;
}
}
qDebug ( ) << "str = " << str;
connect ( ui-> tableWidget, SIGNAL ( cellClicked ( int , int ) ) , this , SLOT ( show_prodcut_sets ( int , int ) ) ) ;
connect ( ui-> tableWidget-> selectionModel ( ) , SIGNAL ( currentChanged ( const QModelIndex & , const QModelIndex & ) ) , SLOT ( doProcessSelLine ( void ) ) ) ;
}
void Widget :: show_prodcut_sets ( int row, int col)
{
QString str;
qDebug ( ) << "所点击的单元格的row=" << row << ",col=" << col;
QTableWidgetItem* item = ui-> tableWidget-> item ( row , col) ;
qDebug ( ) << "内容为:" << item-> text ( ) ;
}
void Widget :: doProcessSelLine ( ) {
int current_column;
QString str;
QTableWidgetItem* item;
current_column = ui-> tableWidget-> currentColumn ( ) ;
for ( int j = 0 ; j < ui-> tableWidget-> columnCount ( ) ; j++ ) {
item = ui-> tableWidget-> item ( current_column, j) ;
str = str + item-> text ( ) + " " ;
}
qDebug ( ) << str<< endl;
}
widget.h
# ifndef WIDGET_H
# define WIDGET_H
# include <QWidget>
# include <QTabWidget>
# include <QTableWidgetItem>
# include <QDebug>
namespace Ui {
class Widget ;
}
class Widget : public QWidget
{
Q_OBJECT
public :
explicit Widget ( QWidget * parent = nullptr ) ;
~ Widget ( ) ;
private :
Ui:: Widget * ui;
void Init ( void ) ;
int row_count, column_count;
private slots:
void show_prodcut_sets ( int , int ) ;
void doProcessSelLine ( void ) ;
} ;
# endif
效果