Qt QTableWidget

1)dialogimpl.h

#ifndef DIALOGIMPL_H
#define DIALOGIMPL_H
//
#include <QDialog>
#include "ui_dialog.h"
#include "QLabel"
#include "QTableWidget"
#include "QHBoxLayout"
//
class DialogImpl : public QDialog, public Ui::Dialog
{
Q_OBJECT
public:
 DialogImpl( QWidget * parent = 0, Qt::WFlags f = 0 );
private:
 QLabel *label;
 QTableWidget *table;
 QHBoxLayout *layout;
private slots:
 void getItemText(QTableWidgetItem* item);
};
#endif

 

2)dialogimpl.cpp

#include "dialogimpl.h"
//
DialogImpl::DialogImpl( QWidget * parent, Qt::WFlags f)
 : QDialog(parent, f)
{
 setupUi(this);
 this->setWindowTitle("QTableWidget Demo");
 
 layout=new QHBoxLayout;
 label=new QLabel;
 label->setFixedWidth(100);
 layout->addWidget(label);
 
 table=new QTableWidget;
 table->setRowCount(3);
 table->setColumnCount(2);
 QStringList headers;
 headers<<"name"<<"level";
 table->setHorizontalHeaderLabels(headers);
 table->setItem(0,0,new QTableWidgetItem(QIcon(":/Resources/Images/line.png"),"line"));
 table->setItem(0,1,new QTableWidgetItem("1"));
 table->setItem(1,0,new QTableWidgetItem(QIcon(":/Resources/Images/rect.png"),"rect"));
 table->setItem(1,1,new QTableWidgetItem("2"));
 table->setItem(2,0,new QTableWidgetItem(QIcon(":/Resources/Images/ellipse.png"),"ellipse"));
 table->setItem(2,1,new QTableWidgetItem("3"));
 layout->addWidget(table);
 connect(table,SIGNAL(currentItemChanged(QTableWidgetItem*,QTableWidgetItem*)),SLOT(getItemText(QTableWidgetItem*)));
 
 this->setLayout(layout);
}
void DialogImpl::getItemText(QTableWidgetItem* item)
{
 int row=item->row();
 int column=item->column();
 label->setText("("+QString::number(row)+","+QString::number(column)+")"+item->text());
}

//

 

3)Images.qrc

<RCC>
    <qresource prefix="/">
        <file>Resources/Images/line.png</file>
        <file>Resources/Images/rect.png</file>
    </qresource>
</RCC>

你可能感兴趣的:(Qt QTableWidget)