AddressBar.cpp Example File
demos/embedded/anomaly/src/AddressBar.cpp
#include "AddressBar.h"
#include <QtCore>
#include <QtGui>
class LineEdit: public QLineEdit
{
public:
LineEdit(QWidget *parent = 0): QLineEdit(parent) {}
void paintEvent(QPaintEvent *event) {
QLineEdit::paintEvent(event);
if (text().isEmpty()) {
QPainter p(this);
int flags = Qt::AlignLeft | Qt::AlignVCenter;
p.setPen(palette().color(QPalette::Disabled, QPalette::Text));
p.drawText(rect().adjusted(10, 0, 0, 0), flags, "Enter address or search terms");
p.end();
}
}
};
AddressBar::AddressBar(QWidget *parent)
: QWidget(parent)
{
m_lineEdit = new LineEdit(parent);
connect(m_lineEdit, SIGNAL(returnPressed()), SLOT(processAddress()));
m_toolButton = new QToolButton(parent);
m_toolButton->setText("Go");
connect(m_toolButton, SIGNAL(clicked()), SLOT(processAddress()));
}
QSize AddressBar::sizeHint() const
{
return m_lineEdit->sizeHint();
}
void AddressBar::processAddress()
{
if (!m_lineEdit->text().isEmpty())
emit addressEntered(m_lineEdit->text());
}
void AddressBar::resizeEvent(QResizeEvent *event)
{
int x, y, w, h;
m_toolButton->adjustSize();
x = width() - m_toolButton->width();
y = 0;
w = m_toolButton->width();
h = height() - 1;
m_toolButton->setGeometry(x, y, w, h);
m_toolButton->show();
x = 0;
y = 0;
w = width() - m_toolButton->width();
h = height() - 1;
m_lineEdit->setGeometry(x, y, w, h);
m_lineEdit->show();
}
void AddressBar::focusInEvent(QFocusEvent *event)
{
m_lineEdit->setFocus();
QWidget::focusInEvent(event);
}