如何在QTabWidget选项卡中选择QTableWidget单元格?

我如何实施:

WbookSsheet代表工作簿和电子表格。我在ExcelMockWidget容器类下实现它们。将Ssheet单元格添加到Wbook选项卡后,我不知道如何访问它们。我知道将Ssheet插入QWidget后会被强制转换为Wbook,但是这有关系吗?

这是我的ExcelView Example存储库。



这是我的课程:

ExcelMockWidget:

class ExcelMockWidget : public QWidget{
    Q_OBJECT
public:
    explicit ExcelMockWidget(QWidget *parent = 0);
    ~ExcelMockWidget();
    void addCell(int row, int column, QVariant cell);
    void addSheet(int index, QString title);
    void setSheetProperties(int rows, int columns, QStringList headers);
signals:
    void indexChanged(int);
public slots:
    void emitIndexChange(int index);
    void select(int row, int column);

private:
    Wbook *book;
    Ssheet *sheet;
};

ExcelMockWidget::ExcelMockWidget(QWidget *parent) : QWidget(parent) {
    book = new Wbook;
    QVBoxLayout *container;
    container = new QVBoxLayout;
    container->addWidget(book);
    setLayout(container);
    connect(book,SIGNAL(changedIndex(int)),this,SLOT(emitIndexChange(int)));
}

ExcelMockWidget::~ExcelMockWidget(){

}

void ExcelMockWidget::addCell(int row, int column, QVariant cell){
    sheet->setCell(row,column,cell);
}

void ExcelMockWidget::addSheet(int index, QString title){
    book->createTab(index, title, sheet);
}

void ExcelMockWidget::setSheetProperties(int rows, int columns, QStringList headers){
    sheet = new Ssheet;
    sheet->setSheetColumnSpan(columns);
    sheet->setSheetRowSpan(rows);
    sheet->setSheetHeaders(headers);
}

void ExcelMockWidget::emitIndexChange(int index){
    emit indexChanged(index);
}

void ExcelMockWidget::select(int row, int column){
    book->selectCell(row, column);
}


Wbook:

namespace Ui {
class Wbook;
}

class Wbook : public QWidget{
    Q_OBJECT

public:
    explicit Wbook(QWidget *parent = 0);
    ~Wbook();
    void createTab(int index, QString title, Ssheet *worksheet);
    void selectTab(int tab);
    void selectCell(int row, int column);
signals:
    void changedIndex(int);
public slots:
    void emitIndexChange(int index);

protected:

private:
    Ui::Wbook *ui;
};
Wbook::Wbook(QWidget *parent) : QWidget(parent), ui(new Ui::Wbook){
    ui->setupUi(this);
    connect(ui->tabWidget,SIGNAL(currentChanged(int)),this,SLOT(emitIndexChange(int)));
}

Wbook::~Wbook(){
    delete ui;
}

void Wbook::createTab(int index, QString title, Ssheet *worksheet){
    ui->tabWidget->insertTab(index, worksheet, title);
}

void Wbook::selectTab(int tab){
    // 0 for no selection and 1 -> for created tabs.
    ui->tabWidget->setCurrentIndex(tab);
}

void Wbook::selectCell(int row, int column){
    ui->tabWidget->currentWidget()->childAt(row+1,column+1)->setFocus();
}

void Wbook::emitIndexChange(int index){
    emit changedIndex(index);
}


表格:

namespace Ui {
class Ssheet;
}

class Ssheet : public QWidget{
    Q_OBJECT
public:
    explicit Ssheet(QWidget *parent = 0);
    ~Ssheet();
    void setSheetTitle(QString title);
    void setSheetRowSpan(int n);
    void setSheetColumnSpan(int n);
    void setSheetHeaders(QStringList headers);
    void setCell(int row, int column, QVariant cell);

    void selectRow(int row);
    void selectCell(int row, int column);
private:
    Ui::Ssheet *ui;
};

Ssheet::Ssheet(QWidget *parent) : QWidget(parent), ui(new Ui::Ssheet){
    ui->setupUi(this);

    ui->table->verticalHeader()->setVisible(false);

    ui->table->setEditTriggers(QAbstractItemView::NoEditTriggers);
    ui->table->setSelectionBehavior(QAbstractItemView::SelectItems);
    ui->table->setSelectionMode(QAbstractItemView::SingleSelection);
    ui->table->setStyleSheet("QTableWidget {selection-background-color: black; selection-color: white}");

    ui->table->setShowGrid(false);
}

Ssheet::~Ssheet(){
    delete ui;
}

void Ssheet::setSheetTitle(QString title){
    ui->table->setObjectName(title);
}

void Ssheet::setSheetRowSpan(int n){
    ui->table->setRowCount(n);
}

void Ssheet::setSheetColumnSpan(int n){
    ui->table->setColumnCount(n);
}

void Ssheet::setSheetHeaders(QStringList headers){
    ui->table->setHorizontalHeaderLabels(headers);
}

void Ssheet::setCell(int row, int column, QVariant cell){
    QTableWidgetItem *input = new QTableWidgetItem;
    input->setData(0,cell);
    ui->table->setItem(row-1,column,input);
}

void Ssheet::selectCell(int row, int column){
    ui->table->setCurrentCell(row,column);
}

最佳答案

似乎只有一个Ssheet硬编码到ExcelMockWidget类中。猜猜您需要在每次对Ssheet的调用上创建新的Wbook::createTab()。像这样:

book->createTab(index, title, new Ssheet(this));


然后在需要时使用Ssheet获取正确的QTabWidget::widget()小部件:

sheet = static_cast<Ssheet*>(QTabWidget::widget(page));


并且...访问其单元!

09-10 00:59