Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

[Solved]QTableWidget functions in a QTabWidget



  • How can I select a QTableWidget cell in a QTabWidget tab?

    How I implement:

    Wbook and Ssheet represent a workbook and spreadsheet. I implement them under ExcelMockWidget container class. I can’t figure out how to access my Ssheet cells once they have been added to a tab. I do understand Ssheet is cast into a QWidget data types once I insert into my Wbook data type, but would that matter?

    Here is my repository for the "ExcelView Example":https://github.com/CCi-BClark/MockExcel .



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

    Ssheet:

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

    @



  • Thanks to Dmitry Markin on "SO":http://stackoverflow.com/a/20956440/3010222.

    I had to make the following edits:

    ExcelMockView:
    @
    void ExcelMockWidget::addSheet(int index, QString title){
    Ssheet *newSheet(sheet);
    book->createTab(index, title, newSheet);
    }

    void ExcelMockWidget::select(int row, int column){
    sheet = static_cast<Ssheet*>(book->getCurrentWidget());
    sheet->selectCell(row, column);
    }
    @

    Wbook:
    @
    QWidget *getCurrentWidget(void);

    QWidget* Wbook::getCurrentWidget(){
    return ui->tabWidget->currentWidget();
    }
    @


Log in to reply