向qt项目添加标记

向qt项目添加标记,qt,combobox,Qt,Combobox,我是QT新手。我正在使用Qt5.3。 我在ui文件中有一个combobox,它显示一组值。如何添加一个功能,使从combobox中选择的每个值显示为带有十字的标记,以便用户可以随时删除标记。最后,它将根据从combobox中所做的选择生成多个标记 提前感谢您的帮助。QComboBox有一个信号,您可以使用该信号查看选择的更改。它还提供所选项目的文本。您可以将此信号连接到添加标签的插槽 至于标签,它们可以只是简单的小部件,包含一个QLabel和一个用于删除标签的QPushButton 下面是一个我

我是QT新手。我正在使用Qt5.3。 我在ui文件中有一个combobox,它显示一组值。如何添加一个功能,使从combobox中选择的每个值显示为带有十字的标记,以便用户可以随时删除标记。最后,它将根据从combobox中所做的选择生成多个标记

提前感谢您的帮助。

QComboBox有一个信号,您可以使用该信号查看选择的更改。它还提供所选项目的文本。您可以将此信号连接到添加标签的插槽

至于标签,它们可以只是简单的小部件,包含一个QLabel和一个用于删除标签的QPushButton

下面是一个我很快总结出来的例子。这是可以改进的,但它应该让你开始。我决定子类QFrame来创建我的标记类

主窗口

mainwindow.cpp

tag.h

tag.cpp


你有什么问题吗?根据combobox选择添加标记,还是设计此标记?两者都是…首先,我无法找到设计标记的方法…然后,我可能可以将从combobox中选择的值分配给标记。那么,是否每次更改combobox选择时都会弹出新标记?如果是这样,是否要限制重复标记的可能性?是…在从combobox中每次新选择后,所选值应显示为新标记..并且在每次选择后,我会将combobox的索引重置为0索引…是,还需要限制重复的可能性tags@NS_19如果它解决了你的问题,你可以。
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

class QHBoxLayout;
class QComboBox;

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

public slots:
    void addTag(const QString& text); // this is a slot we connect our combobox's signal to, so we can add our tags when the selection changes

private:
    Ui::MainWindow *ui;
    QComboBox *cb; // combo box that provides the tag selection
    QHBoxLayout *tag_layout; // layout we use to add the tag widgets to
    bool tagExists(const QString &tag); // we use this function to check if the tag exists already
};

#endif // MAINWINDOW_H
#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QComboBox>
#include <QLayout>
#include "tag.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    QVBoxLayout *main_layout = new QVBoxLayout; // create a new layout
    this->centralWidget()->setLayout(main_layout); // set the layout to our central widget
    cb = new QComboBox(this); // initialize our checkbox
    cb->addItems(QStringList() << "NONE" << "Item 1" << "Item 2" << "Item 3"); // add some random items to our checkbox
    main_layout->addWidget(cb); // add our checkbox to our layout
    connect(cb, SIGNAL(currentIndexChanged(QString)), this, SLOT(addTag(QString))); // connect the checkbox's signal to our slot for adding the items when selection changes
    tag_layout = new QHBoxLayout; // initialize our layout for tags
    main_layout->addLayout(tag_layout); // add our tag layout inside our main layout
}

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

void MainWindow::addTag(const QString &text)
{
    if(cb->currentIndex() == 0) // if it's the first item, do nothing
        return;
    if(tagExists(text)) // if the tag exists, select the first item from the combobox and do nothing else
    {
        cb->setCurrentIndex(0);
        return;
    }
    Tag *tag = new Tag(this); // create our tag widget
    tag->setText(text); // set the tag text
    tag_layout->addWidget(tag); // add the tag widget to our tag layout
    cb->setCurrentIndex(0); // select the first item from the combobox
}

bool MainWindow::tagExists(const QString &tag)
{
    for(int i = 0; i < tag_layout->count(); i++) // go through the items in our tag layout
    {
        Tag *tag_item = dynamic_cast<Tag*>(tag_layout->itemAt(i)->widget()); // try to typecast the item from the layout to Tag
        if(tag_item) // if the item is a tag widget
        {
            if(tag_item->text() == tag) // check the tag's text, if it's the same as the tag we want to add, the tag already exists
                return true;
        }
    }
    return false;
}
#ifndef TAG_H
#define TAG_H

#include <QFrame>

class QLabel;
class QPushButton;

class Tag : public QFrame
{
    Q_OBJECT
public:
    explicit Tag(QWidget *parent = 0);
    ~Tag();
    void setText(const QString &text); // function for setting the text of our tag
    QString text() const; // function for getting the text of our tag

private:
    QLabel *label; // label for showing the text of our tag
    QPushButton *x_button; // the X-button for removing the tag

};

#endif // TAG_H
#include "tag.h"

#include <QLayout>
#include <QLabel>
#include <QPushButton>

Tag::Tag(QWidget *parent) :
    QFrame(parent)
{
    setStyleSheet("Tag{color: #2222FF; background-color: #AAAAFF; border: 2px solid #0000AA; border-radius: 10px;}"
                        "QLabel{color: #2222FF:}"); // we set some basic styling for our tag widget
    label = new QLabel(this); // we initialize our label
    setLayout(new QHBoxLayout); // set a layout for our tag widget
    layout()->addWidget(label); // add our label in the layout
    x_button = new QPushButton(this); // we initialize our X-button
    x_button->setText("X"); // set the text to it.. you could also use an Icon, would probably look a million times better
    x_button->setStyleSheet("QPushButton{color: #2222FF; background-color: #AAAAFF; border: 1px solid #0000AA; border-radius: 10px; text-align: center;}"
                            "QPushButton:hover{color: #FFFFFF; background-color: #5555FF;}"
                            "QPushButton:pressed{color: #FFFFFF; background-color: #0000CC; border-style: inset;}"); // set some styling for our button
    x_button->setFixedSize(20,21); // we set a fixed size for our button
    connect(x_button, SIGNAL(clicked()), this, SLOT(deleteLater())); // delete this widget when the X-button is pressed
    layout()->addWidget(x_button);  // add our button to the layout
    setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum); // set the size policies so our widget won't be ridiculously big
}

Tag::~Tag()
{
}

void Tag::setText(const QString &text)
{
    label->setText(text); // set the tag text to our label
}

QString Tag::text() const
{
    return label->text(); // get our label's text
}