向qt项目添加标签

时间:2014-06-11 04:46:28

标签: qt combobox

我是QT的新手。我正在使用Qt5.3。 我在ui文件中有一个组合框显示一组值。如何添加一个功能,其中从组合框中选择的每个值都显示为带有十字的标记,以便用户可以随时删除标记。最后,它将导致多个标记基于从组合框中完成的选择。

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

QComboBoxcurrentIndexChanged(const QString & text)信号,您可以使用该信号查看选择何时发生变化。它还提供所选项目的文本。您可以将此信号连接到添加标签的插槽。

对于标记,它们可以只是包含QLabelQPushButton的简单小部件,用于删除标记。

这是我快速整理的一个例子。它可以改进,但它应该让你开始。我决定继承QFrame来创建我的Tag类。

<强> mainwindow.h

#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

<强> mainwindow.cpp

#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;
}

<强> tag.h

#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

<强> tag.cpp

#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
}