Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QTableView in MainWindow and tab ordering

Tags:

c++

qt

I Have a problem. When place (for example) two or more QTableViews into MainWindow, no work tab ordering. Why? Because tab is capture by QTableView (pointer to the cell QTableView). How can I send TAB into QMainWindow? I can change focus form one QTableView to another one when tab was pressed.

How can I do it? Can anyone help me please?

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtWidgets>


MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{

    ui->setupUi(this);

    QFileSystemModel *myModel;
    myModel = new QFileSystemModel(this);
    myModel->setReadOnly(true);
    myModel->setRootPath("C:\\");
    ui->tableView->setModel(myModel);
    ui->tableView->setSelectionMode(QAbstractItemView::SingleSelection);
    ui->tableView->setSelectionBehavior(QAbstractItemView::SelectRows);
    ui->tableView->setRootIndex(myModel->index("C:\\"));


    QFileSystemModel *myModel2;
    myModel2 = new QFileSystemModel(this);
    myModel2->setReadOnly(true);
    myModel2->setRootPath("C:\\");
    ui->tableView_2->setModel(myModel2);
    ui->tableView_2->setRootIndex(myModel2->index("C:\\"));

}

MainWindow::~MainWindow()
{
    delete ui;
}
like image 821
exo Avatar asked Jul 09 '13 07:07

exo


1 Answers

If I understand the question correctly, you have two QTableViews inside a single QMainWindow (for instance, using a QGridLayout) and possibly other QWidgets, and you want to navigate the keyboard focus between them, but you can't because the keyboard focus stays on a QTableView (most likely the first one).

If this is the case, you should try these options (possibly together):

First, reimplements the keyPressEvent of your QTableView like this:

void MyTableView::keyPressEvent (QKeyEvent * event)
{
    if(event->key() == Qt::Key_Tab)
        event->ignore();
    else
        QTableView::keyPressEvent(event);
}

Secondly, in the constructor of you table view, disabling keyboard focus (so that the focus will cycle through your other widgets only by using Tab)

void MyTableView::MyTableView()
{
    setFocusPolicy(Qt::ClickFocus); // allow giving focus with mouse click,
                                    // but not with TAB key
}

Finally in the constructor of your QMainWindow, after creating and adding the table views in the layout, you can give your main window the focus (that has be taken by the table views, unless you did the second point), by calling:

void MyMainWindow::MyMainWindow()
{
    setFocus(Qt::OtherFocusReason);
}

Update

The following complete minimal example compiles and works as expected for me: I have two TableViews next to each other on top, and two buttons on bottom. Using Tab cycles between these 4 widgets, without modifying whose cells are selected/highlighted in the TableViews. I can get the focus on a desired TableView either by selecting them with the mouse, or by using Tab. Once the focus is on the the desired TableView, I can move through the cells using the keyboard arrows (or clicking with the mouse).

#include <QtGui>

class TableView: public QTableView
{
    void keyPressEvent(QKeyEvent * event)
    {
        if(event->key() == Qt::Key_Tab)
            event->ignore();
        else
            QTableView::keyPressEvent(event);
    }
};


int main(int argc, char ** argv)
{
    QApplication app(argc, argv);
    QGridLayout * layout = new QGridLayout();

    // the first Model/View pair
    QFileSystemModel * model1 = new QFileSystemModel();
    model1->setRootPath(QDir::rootPath());
    TableView * view1 = new TableView();
    view1->setModel(model1);
    layout->addWidget(view1,0,0);

    // the second Model/View pair
    QFileSystemModel * model2 = new QFileSystemModel();
    model2->setRootPath(QDir::rootPath());
    TableView * view2 = new TableView();
    view2->setModel(model2);
    layout->addWidget(view2,0,1);

    // Other widgets
    layout->addWidget(new QPushButton("Hello"),1,0);
    layout->addWidget(new QPushButton("World"),1,1);

    // Create the window containing them and show it
    QWidget * w = new QWidget();
    w->setLayout(layout);
    w->show();
    return app.exec();
}
like image 184
Boris Dalstein Avatar answered Oct 12 '22 10:10

Boris Dalstein