如何基于QListView中特定的QString内容更改QGraphicsView背景颜色 [英] How to change QGraphicsView background color based on specific QString content inside a QListView

查看:129
本文介绍了如何基于QListView中特定的QString内容更改QGraphicsView背景颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了缩小问题,我举了一个可验证的小gui示例。
如果您想看一眼代码,可以在虽然有用,但无法解决问题,但除此之外,它似乎使用了 QModelIndex 和不确定这个小项目正是我需要的。



我也阅读了,它对于建立和捕获特定且唯一的字符串很有用,但是就改变颜色而言,我无法解决。



我还遇到了使我尝试了以下操作:

  void MainWindow :: changeColorDetection()
{
//如果[信息]最小距离:QListView $ b $内部5 b //比改变QGraphicsView背景的颜色
QModelIndex index = ui-> listView-> currentIndex();
QString itemText = index.data(Qt :: DisplayRole).toString();
if(itemText.startsWith( [INFO]最小距离:10))
{
QColor bg = ui-> graphicsView-> palette()。background()。color ();
ui-> graphicsView-> setStyleSheet(QString( background-color:)+ bg.name(QColor :: HexArgb));
}
}

但是最后一个并没有导致任何变化在后台。



我想念什么?非常感谢您为解决此问题指明了正确的方向。

解决方案

QGraphicsView的后台处理与其他小部件有所不同使用样式表定义背景。使用 ui-> graphicsView-> setBackgroundBrush()更改简单背景,或重载 QGraphicsView :: setBackground()更复杂的事情。在您的情况下:

  ui-> graphicsView-> setBackgroundBrush(QColor( red))

请参见 SVG颜色名称表示有效的颜色名称或使用十六进制表示法。



关于导致颜色变化的文字的含义,您需要注意的是,列表模型具有多个独立的条目,并且您需要为颜色更改建立规则。在您的示例中,它根据当前选定的项目进行了更改。还是要根据最新项目或任何项目进行更改?在这些情况下,您可能需要注意添加和删除项目时的信号,和/或搜索所有项目。直接使用模型非常简单:

  QStringList allStrings = model()-> stringList(); 
QString last = allStrings.last();
// //还可以查看QStringList :: contains(),QStringList :: lastOf()


In order to shrink the problem I made a small verifiable example of a small gui. If you would like to take a glance at the code you can see it here.

I have a specific string on a QLineEdit, this string is passed to a QListView via QPushButton as shown below. Those strings are choices of a QComboBox and they are very specific: 1) "[ INFO] Minimum Distance: 5", 2) "[ INFO] Minimum Distance: 10" and 3) "[ INFO] Minimum Distance: 15"

The problem: How can I detect the specific QString content inside a QListView in order to change the background color of a QGraphicsView?

For example if inside the QListView there is "[ INFO] Minimum Distance: 5", the color of the QGraphicsView should be red or if inside the QListView there is "[ INFO] Minimum Distance: 10", the color of the QGraphicsView should be yellow etc.

mainwindow.cpp

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

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    mView = new QGraphicsView();
    mScene = new QGraphicsScene();
    ui->graphicsView->setScene(mScene);

    mText = new QGraphicsTextItem;
    mText->setPos(150,70);
    mScene->addText(tr("Boat outside alarm area"))->setDefaultTextColor(Qt::black);

    model = new QStringListModel();
    ui->listView->setModel(model);
    ui->listView->setEditTriggers(QAbstractItemView::NoEditTriggers);

    changeColorDetection();

}

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

void MainWindow::changeColorDetection()
{
    QColor red;
    QColor yellow;
    QColor green;

    // if [ INFO] Minimum Distance: 5 inside QListView
    // Than change color of the QGraphicsView background to red

    // if [ INFO] Minimum Distance: 10 inside QListView
    // Than change color of the QGraphicsView background to yellow

    QModelIndex index = ui->listView->currentIndex();
    QString itemText = index.data(Qt::DisplayRole).toString();
    if(itemText.startsWith("[ INFO] Minimum Distance: 10"))
    {
        ui->graphicsView->setStyleSheet("QGraphicsView {background-color: red}");
    }
}

void MainWindow::on_pushButton_clicked()
{
    QString str = ui->lineEdit->text();
    model->insertRow(model->rowCount());
    QModelIndex index = model->index(model->rowCount()-1);
    model->setData(index, str);
}


void MainWindow::on_comboBox_currentIndexChanged(const QString &arg1)
{
    QString list = ui->comboBox->currentText();
    ui->lineEdit->setText(list);
    Q_UNUSED(arg1)
}

mainwindow.h

#include <QGraphicsView>
#include <QGraphicsScene>
#include <QGraphicsTextItem>
#include <QStringListModel>

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();
    void changeColorDetection();

private slots:
    void on_pushButton_clicked();
    void on_comboBox_currentIndexChanged(const QString &arg1);

private:
    Ui::MainWindow *ui;
    QGraphicsView *mView;
    QGraphicsScene *mScene;
    QGraphicsTextItem *mText;
    QStringListModel *model;

};
#endif // MAINWINDOW_H

main.cpp

#include "mainwindow.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    return a.exec();
}

In case you also would like to see the small .ui the code is below:

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>555</width>
    <height>382</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralwidget">
   <layout class="QGridLayout" name="gridLayout_2">
    <item row="0" column="0">
     <layout class="QVBoxLayout" name="verticalLayout">
      <item>
       <widget class="QGroupBox" name="groupBox">
        <property name="title">
         <string>Area</string>
        </property>
        <layout class="QGridLayout" name="gridLayout">
         <item row="0" column="0">
          <layout class="QHBoxLayout" name="horizontalLayout">
           <item>
            <widget class="QCheckBox" name="checkBoxRedArea">
             <property name="text">
              <string>Red area</string>
             </property>
            </widget>
           </item>
           <item>
            <widget class="QCheckBox" name="checkBoxYellowArea">
             <property name="text">
              <string>Yellow Area</string>
             </property>
            </widget>
           </item>
           <item>
            <widget class="QCheckBox" name="checkBoxGreenArea">
             <property name="text">
              <string>Green Area</string>
             </property>
            </widget>
           </item>
          </layout>
         </item>
         <item row="1" column="0">
          <layout class="QHBoxLayout" name="horizontalLayout_3">
           <item>
            <widget class="QPushButton" name="pushButton">
             <property name="text">
              <string>Add Message</string>
             </property>
            </widget>
           </item>
           <item>
            <widget class="QLineEdit" name="lineEdit"/>
           </item>
           <item>
            <widget class="QComboBox" name="comboBox">
             <item>
              <property name="text">
               <string>Select Option Distance</string>
              </property>
             </item>
             <item>
              <property name="text">
               <string>[ INFO] Minimum Distance: 5</string>
              </property>
             </item>
             <item>
              <property name="text">
               <string>[ INFO] Minimum Distance: 10</string>
              </property>
             </item>
             <item>
              <property name="text">
               <string>[ INFO] Minimum Distance: 15</string>
              </property>
             </item>
             <item>
              <property name="text">
               <string>[ INFO] Minimum Distance: 20</string>
              </property>
             </item>
            </widget>
           </item>
          </layout>
         </item>
        </layout>
       </widget>
      </item>
      <item>
       <layout class="QHBoxLayout" name="horizontalLayout_2">
        <item>
         <widget class="QListView" name="listView"/>
        </item>
        <item>
         <widget class="QGraphicsView" name="graphicsView">
          <property name="styleSheet">
           <string notr="true">background-color: rgb(211, 215, 207);</string>
          </property>
         </widget>
        </item>
       </layout>
      </item>
     </layout>
    </item>
   </layout>
  </widget>
  <widget class="QMenuBar" name="menubar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>555</width>
     <height>22</height>
    </rect>
   </property>
  </widget>
  <widget class="QStatusBar" name="statusbar"/>
 </widget>
 <resources/>
 <connections/>
</ui>

What I have done so far:

I have been doinf a lot of research about this problem and came across this source which was useful but could not solve the problem, but in addition it seems to use a QModelIndex and I am not sure this is exactly what I need for this small project.

Also I read this source which was useful to establish and capture the specific and unique string but in terms of changing colors I could not solve that.

I also came across this which led me to give a try to the following:

void MainWindow::changeColorDetection()
{
    // if [ INFO] Minimum Distance: 5 inside QListView
    // Than change color of the QGraphicsView background
    QModelIndex index = ui->listView->currentIndex();
    QString itemText = index.data(Qt::DisplayRole).toString();
    if(itemText.startsWith("[ INFO] Minimum Distance: 10"))
    {
        QColor bg = ui->graphicsView->palette().background().color();
        ui->graphicsView->setStyleSheet(QString("background-color:") + bg.name(QColor::HexArgb));
    }
}

But also this last one did not result in any change in the background.

What am I missing? Thank you very much for pointing to the right direction for solving this issue.

解决方案

QGraphicsView background handling differs from other widgets where you might use stylesheets to define the background. Use ui->graphicsView->setBackgroundBrush() to change a simple background, or overload QGraphicsView::setBackground() for anything more complicated. In your case:

ui->graphicsView->setBackgroundBrush(QColor("red"))

See SVG color names for valid color names or use hex notation.

Regarding the dection of texts that lead to color changes, you need to be aware that a list model has multiple independent entries and you need to establish a rule for the color change. In your example, it is changed dependent on the currently selected item. Or do you want to change it based on the latest item, or any item? In these cases you might need to watch for signals when items are added and removed, and/or to search through all items. This is very easy using your model directly:

QStringList allStrings = model()->stringList();
QString last = allStrings.last();
// also have a look at QStringList::contains(), QStringList::lastOf()

这篇关于如何基于QListView中特定的QString内容更改QGraphicsView背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆