【问题标题】:scrollbar Qt, can't scroll滚动条Qt,无法滚动
【发布时间】:2014-08-20 11:13:33
【问题描述】:

谁能告诉我为什么我可以从我的QTableView 看到滚动条,但不能使用它? 有人有想法吗?

我的代码:

View::View()
{
}

void View::init()
{

    // add model and Table

    myModel= new Model();
    QTableView *myView=new QTableView( this );
    myView->setModel(myModel);
    QWidget *mywidget= new QWidget(this);

    // add buttons and labels

    QPushButton* btnaddtotable = new QPushButton( "Add to Table",this);
    btnaddtotable->setGeometry(50,20,100,40);
    QPushButton* btnFile = new QPushButton( "Open file",this);
    btnFile->setGeometry(50,20,100,40);
    QPushButton* btnOpenFile = new QPushButton( "Search file",this);
    btnFile->setGeometry(50,20,100,40);
    btnOpenFile->setGeometry(50,20,100,40);

    // connections

    QObject::connect ( btnaddtotable, SIGNAL ( clicked() ), this , SLOT(pushButtonClicked())) ;
    QObject::connect ( btnFile, SIGNAL ( clicked() ), this , SLOT(FileButtonClicked())) ;
    QObject::connect ( btnOpenFile, SIGNAL ( clicked() ), this , SLOT(OpenFileButtonClicked()));

    // named the buttons and labels

    myView->setObjectName(QStringLiteral("Table"));
    myView->setGeometry(QRect(20, 100, 361, 191));
    txtname = new QLineEdit(mywidget);
    txtname->setObjectName(QStringLiteral("txtname"));
    txtname->setGeometry(QRect(20, 30, 151, 31));
    txtvalue = new QLineEdit(mywidget);
    txtvalue->setObjectName(QStringLiteral("textEdit"));
    txtvalue->setGeometry(QRect(230, 30, 151, 31));
    label = new QLabel ("Name: ", mywidget );
    label->setObjectName(QStringLiteral("label"));
    label->setGeometry(QRect(20, 10, 61, 20));
    label_2 = new QLabel ("Value ", mywidget );
    label_2->setObjectName(QStringLiteral("label_2"));
    label_2->setGeometry(QRect(235, 10, 51, 20));
    QLabel *label3 = new QLabel ("DateiPfad: ", mywidget );
    label3->setObjectName(QStringLiteral("label"));
    label3->setGeometry(QRect(20, 305, 61, 20));
    txtPfad = new QLineEdit(mywidget);
    txtPfad->setObjectName(QStringLiteral("txtPfad"));
    txtPfad->setGeometry(QRect(80, 300, 300, 31));

    btnaddtotable->setObjectName(QStringLiteral("pushButton"));
    btnaddtotable->setGeometry(QRect(240, 70, 75, 23));
    btnFile->setGeometry(QRect(160, 70, 75, 23));
    btnOpenFile->setGeometry(QRect(80, 70, 75, 23));


    // searching the strings


    this->setGeometry(500,500,400,350);

    // for sorting
    sort_filter = new QSortFilterProxyModel(this);
    sort_filter->setSourceModel(myModel);
    // sort the first column
    sort_filter->sort(0);

    myView->setColumnWidth(0,179);
    myView->setColumnWidth(1,180);
    myView->setModel (sort_filter);
    // to check if exists
    sort_filter->setSourceModel(myModel);
    //myView->setCornerButtonEnabled(true);
    //myView->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
    myView->setAutoScroll(true);
    myView->setAutoScrollMargin(20);
    myView->setHorizontalScrollMode(QAbstractItemView::ScrollPerItem);

}

我忘记了什么吗?

【问题讨论】:

  • 这确实很奇怪。我确认了这个问题。我正在尝试将代码减少到最少以显示问题,然后更容易找到解决方案。
  • 请删除自动滚动,看看是否有效。
  • 奇怪:我可以添加一个滚动区域,但是我看不到 QTextEdits 和标签
  • 不,当我删除自动滚动时。什么都没有改变..
  • 所以这很奇怪。我发现如果我删除除表格和行编辑之外的所有内容,滚动将不起作用。如果我进一步删除 setGeometry 进行行编辑,滚动就可以了!

标签: c++ qt scroll qtableview


【解决方案1】:

您有一堆相互重叠的小部件。我建议使用布局,这样你就不会遇到这样的问题。

但主要问题是您的QWidget *mywidget 对象。您没有为其设置几何图形,因此它最终会覆盖您的QTableView。例如,如果您为它设置一个带有红色背景的样式表,您会看到这种情况发生。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-25
    • 2016-10-30
    • 2015-05-12
    • 2017-09-25
    • 2018-04-29
    • 2013-02-26
    相关资源
    最近更新 更多