QTableWidget 在 QTabWidget 中起作用



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

我如何实现:

WbookSsheet表示工作簿和电子表格。我在容器类ExcelMockWidget实现它们。一旦将Ssheet单元格添加到Wbook选项卡中,我就无法弄清楚如何访问它们。 我确实知道一旦我插入Wbook Ssheet就会被投射到QWidget中,但这有关系吗?

这是我的ExcelView示例存储库。


这是我的课程:

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);
}

文书:

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类中。 猜猜您需要在每次调用Wbook::createTab()时创建新Ssheet。喜欢这个:

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

然后在需要时获得带有QTabWidget::widget()的适当Ssheet小部件:

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

而且......访问它的细胞!

相关内容

  • 没有找到相关文章

最新更新