UI布局



本例子展示自定义数据类型的注册与QDialogButtonBox的应用。
textdialog.h

#ifndef TEXTDIALOG_H
#define TEXTDIALOG_H

#include <QDialog>
#include <qabstractbutton.h>

#include "textandnumber.h"

namespace Ui {
class TextDialog;
}

class TextDialog : public QDialog
{
    Q_OBJECT

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

public slots:
    void showText( TextAndNumber tan );

private slots:
    void buttonClicked( QAbstractButton *button );

    void on_pushButton_clicked();
signals:
    void getInfo( TextAndNumber tan );

private:
    int count;
    QMutex mutex;
    Ui::TextDialog *ui;
};

#endif // TEXTDIALOG_H

textdialog.cpp

#include "textdialog.h"
#include "ui_textdialog.h"

#include <QDebug>
#include <QMutexLocker>

TextDialog::TextDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::TextDialog)
{
    ui->setupUi(this);
    count = 0;

    connect( ui->buttonBox, SIGNAL(clicked(QAbstractButton*)),
             this, SLOT(buttonClicked(QAbstractButton*)) );
    connect( this, SIGNAL( getInfo(TextAndNumber)),
             this, SLOT(showText(TextAndNumber)) );
}

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

void TextDialog::showText(TextAndNumber tan)
{
    QMutexLocker locker( &mutex );
    ui->listWidget->addItem( QString( "Call %1 (%3): %2" ).arg( count++ ).arg(tan.text).arg(tan.number) );
}

void TextDialog::buttonClicked(QAbstractButton *button)
{
    qDebug() << "buttonClicked: " << ui->buttonBox->buttonRole( button );
    if( ui->buttonBox->buttonRole( button ) == QDialogButtonBox::ResetRole )
    {
        ui->listWidget->clear();
    }
}

void TextDialog::on_pushButton_clicked()
{
    emit getInfo( TextAndNumber( 1, "one") );
}

整个工程的地址:
https://github.com/theArcticOcean/qtLib/tree/master/TextDialog

分类: C plus plus

发表评论

电子邮件地址不会被公开。 必填项已用*标注