Just a guess:
if another button on the widget is pressed, or the widget is resized, the QLineEdit looses focus. So you could just override QLineEdit::focusOutEvent() and close/hide the (now custom) line edit.
I did my own implementation of this poblem, but the QLineEdit is just floating above the tab. also it closes itself if its done and if the user presses esc, the entered text is discarded. an event "publishText" is emitted, if the user successfully entered text. Its not perfect, but it works. If you have any suggestions, go ahead.
class IndependentLineEdit
: public QLineEdit { Q_OBJECT
public:
IndependentLineEdit
(QWidget* parent
= 0);
public slots:
signals:
void newTextEntered
(const QString &text
);
protected:
private slots:
void publishText();
void setupWidget();
};
IndependentLineEdit
::IndependentLineEdit(QWidget* parent
) : setupWidget();
}
IndependentLineEdit
::IndependentLineEdit(const QString & contents,
QWidget* parent
) : setupWidget();
}
void IndependentLineEdit
::setText(const QString &text
) { setFocus();
selectAll();
show();
}
void IndependentLineEdit
::keyPressEvent(QKeyEvent* event
) { if (event->key() == Qt::Key_Escape) {
setText("");
hide();
}
}
void IndependentLineEdit
::focusOutEvent(QFocusEvent* event
) { emit editingFinished();
}
void IndependentLineEdit::publishText() {
if (text() != "") {
emit newTextEntered(text());
}
hide();
}
void IndependentLineEdit::setupWidget() {
connect(this, SIGNAL(editingFinished()), this, SLOT(publishText()));
setWindowFlags(Qt::CustomizeWindowHint);
}
class IndependentLineEdit: public QLineEdit {
Q_OBJECT
public:
IndependentLineEdit(QWidget* parent = 0);
IndependentLineEdit(const QString & contents, QWidget* parent = 0 );
public slots:
void setText(const QString &text);
signals:
void newTextEntered(const QString &text);
protected:
void keyPressEvent(QKeyEvent* event);
void focusOutEvent(QFocusEvent* event);
private slots:
void publishText();
void setupWidget();
};
IndependentLineEdit::IndependentLineEdit(QWidget* parent) :
QLineEdit(parent) {
setupWidget();
}
IndependentLineEdit::IndependentLineEdit(const QString & contents, QWidget* parent) :
QLineEdit(contents, parent) {
setupWidget();
}
void IndependentLineEdit::setText(const QString &text) {
QLineEdit::setText(text);
setFocus();
selectAll();
show();
}
void IndependentLineEdit::keyPressEvent(QKeyEvent* event) {
QLineEdit::keyPressEvent(event);
if (event->key() == Qt::Key_Escape) {
setText("");
hide();
}
}
void IndependentLineEdit::focusOutEvent(QFocusEvent* event) {
QLineEdit::focusOutEvent(event);
emit editingFinished();
}
void IndependentLineEdit::publishText() {
if (text() != "") {
emit newTextEntered(text());
}
hide();
}
void IndependentLineEdit::setupWidget() {
connect(this, SIGNAL(editingFinished()), this, SLOT(publishText()));
setWindowFlags(Qt::CustomizeWindowHint);
}
To copy to clipboard, switch view to plain text mode
I use it with a custom QTabBar and a custom QTabWidget:
class EditableTabBar
: public QTabBar {Q_OBJECT
public:
EditableTabBar
(QWidget* parent
= 0);
virtual ~EditableTabBar();
signals:
void tabTextChanged
(int index,
const QString &text
);
protected:
private slots:
void setCurrentTabText
(const QString &text
);
private:
IndependentLineEdit _edit;
};
EditableTabBar
::EditableTabBar(QWidget* parent
) : connect(&_edit,
SIGNAL(newTabTextEntered
(const QString &)),
this,
SLOT( setCurrentTabText
(const QString &)));
}
EditableTabBar::~EditableTabBar() {
// TODO Auto-generated destructor stub
}
void EditableTabBar
::mouseDoubleClickEvent(QMouseEvent* event
) { _edit.setText(tabText(currentIndex()));
_edit.move(mapToGlobal(event->pos()));
}
void EditableTabBar
::setCurrentTabText(const QString &text
) { setTabText(currentIndex(), text);
emit tabTextChanged(currentIndex(), text);
}
class EditableTabBar: public QTabBar {
Q_OBJECT
public:
EditableTabBar(QWidget* parent = 0);
virtual ~EditableTabBar();
signals:
void tabTextChanged(int index, const QString &text);
protected:
void mouseDoubleClickEvent(QMouseEvent* event);
private slots:
void setCurrentTabText(const QString &text);
private:
IndependentLineEdit _edit;
};
EditableTabBar::EditableTabBar(QWidget* parent) :
QTabBar(parent), _edit("", 0) {
connect(&_edit, SIGNAL(newTabTextEntered(const QString &)), this,
SLOT( setCurrentTabText(const QString &)));
}
EditableTabBar::~EditableTabBar() {
// TODO Auto-generated destructor stub
}
void EditableTabBar::mouseDoubleClickEvent(QMouseEvent* event) {
_edit.setText(tabText(currentIndex()));
_edit.move(mapToGlobal(event->pos()));
}
void EditableTabBar::setCurrentTabText(const QString &text) {
setTabText(currentIndex(), text);
emit tabTextChanged(currentIndex(), text);
}
To copy to clipboard, switch view to plain text mode
the tabTextChanged() signal is needed for my application, but its not needed for everything to work.
The tabwidget:
Q_OBJECT
public:
EditableTabWidget
(QWidget* parent
= 0);
signals:
void tabTextChanged
(int index,
const QString &text
);
};
EditableTabWidget
::EditableTabWidget(QWidget* parent
) : EditableTabBar* t = new EditableTabBar();
connect(t,
SIGNAL(tabTextChanged
(int,
const QString &)),
this,
SIGNAL(tabTextChanged
(int,
const QString &)));
setTabBar(t);
}
class EditableTabWidget : public QTabWidget {
Q_OBJECT
public:
EditableTabWidget(QWidget* parent = 0);
signals:
void tabTextChanged(int index,const QString &text);
};
EditableTabWidget::EditableTabWidget(QWidget* parent) :
QTabWidget(parent) {
EditableTabBar* t = new EditableTabBar();
connect(t, SIGNAL(tabTextChanged(int, const QString &)), this,
SIGNAL(tabTextChanged(int, const QString &)));
setTabBar(t);
}
To copy to clipboard, switch view to plain text mode
Bookmarks