QLineEdit eventFilter中的QPushButton颜色更改

时间:2018-03-08 01:25:03

标签: c++ qt qt5 qpushbutton qlineedit

当我在其中一个lineedit中插入一个数字时,我想显示一个按钮。按钮和行标位于同一行。我知道lineedit的位置或名称,但我不知道如何链接回按钮使其可见或能够改变颜色。

如果你查看eventFilter,那就是我被困的地方,我需要一些帮助。 谢谢

#include "mainwindow.h"
#include "ui_mainwindow.h" 
#include <QLineEdit>
#include <QPushButton>
#include <QGridLayout>
#include <QLabel>
#include <QRegion>
#include <QLayoutItem>
#include <QList>
#include<QObject>
#include <QEvent>
#include <QKeyEvent>
#include <QModelIndexList>
#include <QKeySequence>
#include <QSignalMapper>
#include<QIntValidator>
#include <QDebug>

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


        ui->scrollArea->setVerticalScrollBarPolicy( Qt::ScrollBarAlwaysOn );
        ui->scrollArea->setWidgetResizable( true );

        QWidget *widget = new QWidget(this);
        ui->scrollArea->setWidget( widget );

        QGridLayout *gridLayout = new QGridLayout();
        widget->setLayout( gridLayout );


    QPushButton *button[20];

    for (int i = 0; i < 20; i++)
    {
        QLabel *label = new QLabel( QString( "%1" ).arg( i ) );
        gridLayout->addWidget(label,i,1,1,1 );
        gridLayout->setColumnMinimumWidth(1, 100);
        gridLayout->setColumnMinimumWidth(2, 10);

        if (i==5)
        {
            QLineEdit *lineEdit = new QLineEdit;
            gridLayout->addWidget(lineEdit,i,5,1,1);

        } else
        {
        QLineEdit *lineEdit = new QLineEdit(this);
        gridLayout->addWidget(lineEdit,i,3,1,1);
        lineEdit->setValidator(new QIntValidator(0,100,this));
        lineEdit->setObjectName(QString::number(i));
        lineEdit->installEventFilter(this);


        gridLayout->setColumnMinimumWidth(4, 25);
        gridLayout->setColumnMinimumWidth(5, 50);
        gridLayout->setColumnMinimumWidth(6, 25);

        QLineEdit *lineEdit_B = new QLineEdit;
        gridLayout->addWidget(lineEdit_B,i,7,1,1);
        lineEdit_B->setValidator(new QIntValidator(0,100,this));

        gridLayout->setColumnMinimumWidth(8, 10);
        }


        gridLayout->setColumnMinimumWidth(9, 10);
        button[i] = new QPushButton();
        gridLayout->addWidget(button[i],i,10,1,1);
        gridLayout->setColumnStretch(10,20);

        button[i]->setFixedHeight(20);
        button[i]->setFixedWidth(20);
        button[i]->setStyleSheet("background-color:red;");
        button[i]->setText(QString::number(i));

        QRegion* region = new QRegion(*(new QRect(button[i]->x(),button[i]->y(),15,15)),QRegion::Ellipse);
        button[i]->setMask(*region);
        button[i]->setVisible(false);

        gridLayout->setColumnMinimumWidth(10, 50);
        gridLayout->setColumnMinimumWidth(11, 10);

    }

  }


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


bool MainWindow::eventFilter(QObject * obj, QEvent *event)// *event)
{
    if (event->type() == QEvent::KeyPress)
    {
      ui->lineEdit->setText(QString("%1").arg(obj->objectName()));
      QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
          qDebug() << "key " << keyEvent->key() << "from" << obj;

  // HERE'S WHERE I NEED HELP
      QPushButton* button = ui->scrollArea->findChild<QPushButton*>();  

  // I think here I would use a for loop to match the listedit 
  // with a number with the corresponding push button.
  // When i find the push button I set it to yellow.

   }
   return QObject::eventFilter(obj, event);

   }




   void MainWindow::on_pushButton_clicked()
   {

   }

1 个答案:

答案 0 :(得分:1)

没有必要使用eventFilter,因为很难区分它是哪个元素,所以你会使任务复杂化。一种可能的选择是使用textChanged信号来显示按钮。

此外,您有许多在循环中不必要地执行的任务,那些不依赖于索引的任务必须已经完成。

此外,如果您要存储按钮,请不要使用数组,请使用像QList这样的容器。

此外,您不应该不加区别地创建指针,因为您有责任消除它们,例如,QRegion通过值传递给setMask(),因此不必创建指针。

#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QGridLayout>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QScrollArea>

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

    ui->scrollArea->setVerticalScrollBarPolicy( Qt::ScrollBarAlwaysOn );
    ui->scrollArea->setWidgetResizable( true );

    QWidget *widget = new QWidget(this);
    ui->scrollArea->setWidget( widget );

    QGridLayout *gridLayout = new QGridLayout(widget);

    for(int i=0; i<20; i++){
        QLabel *label = new QLabel(QString::number(i));
        gridLayout->addWidget(label, i, 1);

        QPushButton *button = new QPushButton(QString::number(i));
        gridLayout->addWidget(button,i,10,1,1);
        button->setFixedSize(20, 20);
        button->setStyleSheet("background-color:red;");

        QRegion region(QRect(button->pos(),QSize(15,15)),QRegion::Ellipse);
        button->setMask(region);
        button->hide();

        if(i==5){
            QLineEdit *lineEdit = new QLineEdit;
            gridLayout->addWidget(lineEdit,i,5);

            connect(lineEdit, &QLineEdit::textChanged, [button](const QString &text){
                button->setVisible(!text.isEmpty());
            });
        }
        else{
            QLineEdit *lineEdit_A = new QLineEdit;
            gridLayout->addWidget(lineEdit_A,i,3);
            lineEdit_A->setValidator(new QIntValidator(0,100,this));

            QLineEdit *lineEdit_B = new QLineEdit;
            gridLayout->addWidget(lineEdit_B,i, 7);
            lineEdit_B->setValidator(new QIntValidator(0,100,this));

            connect(lineEdit_A, &QLineEdit::textChanged, [button](const QString &text){
                button->setVisible(!text.isEmpty());
            });
            connect(lineEdit_B, &QLineEdit::textChanged, [button](const QString &text){
                button->setVisible(!text.isEmpty());
            });
        }
    }

    gridLayout->setColumnMinimumWidth(1, 100);
    gridLayout->setColumnMinimumWidth(2, 10);
    gridLayout->setColumnMinimumWidth(4, 25);
    gridLayout->setColumnMinimumWidth(5, 50);
    gridLayout->setColumnMinimumWidth(6, 25);
    gridLayout->setColumnMinimumWidth(8, 10);
    gridLayout->setColumnMinimumWidth(9, 10);
    gridLayout->setColumnStretch(10,20);
    gridLayout->setColumnMinimumWidth(10, 50);
    gridLayout->setColumnMinimumWidth(11, 10);
}

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

完整示例可在以下link中找到。