0

我是一个 Qt 新手,我要做的就是创建一个QLineEdit带有一些自定义项(默认对齐和默认文本)的自定义类。现在我只是想建立一个基类,只继承QWidget. 这就是我所拥有的(我知道非常糟糕的代码):

用户文本(utxt.h):

#ifndef UTXT_H
#define UTXT_H

#include <QWidget>
#include <QLineEdit>

class utxt : public QWidget

{
    Q_OBJECT
public:
    explicit utxt(QWidget *parent = 0);

    QString text () const;
    const QString displayText;

    Qt::Alignment   alignment;
    void setAlignment(Qt::Alignment);

signals:

public slots:

};

#endif // UTXT_H

utxt.cpp:

#include "utxt.h"

utxt::utxt(QWidget *parent) :
    QWidget(parent)
{
    QString utxt::text()
    {
        return this->displayText;
    }

    void utxt::setAlignment(Qt::Alignment align)
    {
       this->alignment = align;
    }
}

我知道这确实是错误的,并且我在 utxt.cpp 中的两个函数上不断收到“本地函数定义是非法的”错误。有人可以指出我正确的方向吗?我只是想创建一个自定义QLineEdit来推广我的其他行编辑。

4

1 回答 1

0

QLineEdit已经具有可以设置的对齐方式以及placeholderText

LE:正如我所说,这个功能不需要继承自QLineEdit(或QWidget),但如果你真的想这样做,你可以创建你的类并编写一个构造函数,它接受你想要的参数并调用QLineEdit它的功能, 就像是:

//in the header
//... i skipped the include guards and headers 
class utxt : public QLineEdit
{
    Q_OBJECT
public:
//you can provide default values for all the parameters or hard code it into the calls made from the constructor's definition
    utxt(const QString& defaultText = "test text", Qt::Alignment align = Qt::AlignRight, QWidget *parent = 0);
};

//in the cpp
utxt::utxt(const QString& defaultText, Qt::Alignment alignement, QWidget *parent) :     QLineEdit(parent)
{
//call setPlaceHolder with a parameter or hard-code the default
    setPlaceholderText(defaultText); 
//same with the default alignement
    setAlignment(alignement); 
}
于 2013-06-21T06:30:14.287 回答