C++ GUI Qt4编程中项视图类的代码
coordinatesetter.h文件
#include
#include
#include
class QPushButton;
class QTableWidget;
class CoordinateSetter : public QDialog
{
Q_OBJECT
public:
CoordinateSetter(QList *coords, QWidget *parent = 0);
// void done(int result);
private slots:
void addRow();
private:
QTableWidget *tableWidget;
QPushButton *addRowButton;
QPushButton *okButton;
QPushButton *cancelButton;
QList *coordinates;
};
coordinatesetter.cpp文件
#include
#include "coordinatesetter.h"
CoordinateSetter::CoordinateSetter(QList *coords,
QWidget *parent)
: QDialog(parent)
{
coordinates = coords;
tableWidget = new QTableWidget(0, 2);
tableWidget->setHorizontalHeaderLabels(
QStringList() << tr("X") << tr("Y"));//设置了水平头标签
for (int row = 0; row < coordinates->count(); ++row) {//row小于写入的值coordinates的列表数量
QPointF point = coordinates->at(row); //浮点数指向一行的位置
addRow();
tableWidget->item(row, 0)->setText(QString::number(point.x()));//将浮点数x轴写入第0列QTableWidgetItem * QTableWidget::item ( int row, int column ) const
tableWidget->item(row, 1)->setText(QString::number(point.y()));//将浮点数y轴写入第1列。将pointx.x()转换为字符串型
}
//QString::number 将数据转换为字符串String QString::number ( long n, int base = 10 )
addRowButton = new QPushButton(tr("&Add Row"));
okButton = new QPushButton(tr("OK"));
okButton->setDefault(true);
cancelButton = new QPushButton(tr("Cancel"));
connect(addRowButton, SIGNAL(clicked()), this, SLOT(addRow()));
connect(okButton, SIGNAL(clicked()), this, SLOT(accept()));
connect(cancelButton, SIGNAL(clicked()), this, SLOT(reject()));
QHBoxLayout *buttonLayout = new QHBoxLayout;//水平布局
buttonLayout->addWidget(addRowButton);
buttonLayout->addStretch();
buttonLayout->addWidget(okButton);
buttonLayout->addWidget(cancelButton);
QVBoxLayout *mainLayout = new QVBoxLayout;//垂直布局
mainLayout->addWidget(tableWidget);
mainLayout->addLayout(buttonLayout); //将水平布局中的控件加入到垂直布局中来
setLayout(mainLayout); //确定设置了以上布局
setWindowTitle(tr("Coordinate Setter"));
}
/*
void CoordinateSetter::done(int result)
{
if (result == QDialog::Accepted) {
coordinates->clear();
for (int row = 0; row < tableWidget->rowCount(); ++row) {
double x = tableWidget->item(row, 0)->text().toDouble();
double y = tableWidget->item(row, 1)->text().toDouble();
coordinates->append(QPointF(x, y));
}
}
QDialog::done(result);
}
*/
void CoordinateSetter::addRow() //添加行并且设置了表格中数据的位置
{
int row = tableWidget->rowCount();//行的数量在表格中
tableWidget->insertRow(row); //在上面的行数下插入一个空行到表格中
QTableWidgetItem *item0 = new QTableWidgetItem;
item0->setTextAlignment(Qt::AlignRight | Qt::AlignVCenter); //设置数据在表格中的位置位靠右,居中
tableWidget->setItem(row, 0, item0); //设置表格中每行中的第1列中的数据是按照item0设置的
QTableWidgetItem *item1 = new QTableWidgetItem;
item1->setTextAlignment(Qt::AlignRight | Qt::AlignVCenter);
tableWidget->setItem(row, 1, item1);
//tableWidget->setCurrentItem(item0);
main.cpp
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QList coordinates;
coordinates << QPointF(0.0, 0.9) //将QPointF设定的数值放进QList类清单里面去
<< QPointF(0.2, 11.0)
<< QPointF(0.4, 15.4)
<< QPointF(0.6, 12.9)
<< QPointF(0.8, 8.5)
<< QPointF(1.0, 7.1)
<< QPointF(1.2, 4.0)
<< QPointF(1.4, 13.6)
<< QPointF(1.6, 22.2)
<< QPointF(1.8, 22.2);
CoordinateSetter coordinateSetter(&coordinates);
coordinateSetter.show();
return app.exec();
}
阅读(2607) | 评论(0) | 转发(0) |