I implement a "google-search bar" as below,hope it will help others
//mytoolbar.h
#ifndef MYTOOLBAR_
#include <QComboBox>
#include <QPushButton>
#include <QListWidget>
{
public:
{
setViewportMargins(0,0,0,30); //we'll put "clear history" button in the bottom margin
}
};
{
public:
void showPopup ();
void hidePopup ();
private:
myQListWidget *te_;
};
#endif
//mytoolbar.h
#ifndef MYTOOLBAR_
#include <QComboBox>
#include <QPushButton>
#include <QListWidget>
class myQListWidget:public QListWidget
{
public:
myQListWidget(QWidget * parent = 0 ):QListWidget(parent)
{
setViewportMargins(0,0,0,30); //we'll put "clear history" button in the bottom margin
}
};
class myToolBar:public QComboBox
{
public:
myToolBar(QWidget * parent = 0);
void showPopup ();
void hidePopup ();
private:
QPushButton *bt_;
myQListWidget *te_;
};
#endif
To copy to clipboard, switch view to plain text mode
#include "mytoolbar.h"
{
setStyleSheet("QPushButton {text-align:center;color:blue;text-decoration:underline;font-family:arial; background-color:#d4d0c8; }");
setEditable(true);
setFrame(false);
resize(300,size().height());
te_ = new myQListWidget(this);
setView(te_);
setModel(te_->model());
te_->viewport()->setBackgroundRole(backgroundRole()); //change the background color of popup list
bt_->setFlat(true);
bt_->setVisible(false);
connect(bt_,SIGNAL(clicked(bool)),this,SLOT(clear()));
}
void myToolBar::showPopup ()
{
bt_->setVisible(true);
//move button to right-down corner
QRect qRect
(view
()->geometry
());
int iXpos=qRect.width()-bt_->width();
int iYpos=qRect.height()-bt_->height();
bt_->move(iXpos,iYpos);
}
void myToolBar::hidePopup ()
{
bt_->setVisible(false);
}
#include "mytoolbar.h"
myToolBar::myToolBar(QWidget * parent ):QComboBox(parent)
{
setStyleSheet("QPushButton {text-align:center;color:blue;text-decoration:underline;font-family:arial; background-color:#d4d0c8; }");
setEditable(true);
setFrame(false);
resize(300,size().height());
te_ = new myQListWidget(this);
setView(te_);
setModel(te_->model());
te_->viewport()->setBackgroundRole(backgroundRole()); //change the background color of popup list
bt_ = new QPushButton("clear hisroty",view());
bt_->setFlat(true);
bt_->setVisible(false);
connect(bt_,SIGNAL(clicked(bool)),this,SLOT(clear()));
}
void myToolBar::showPopup ()
{
QComboBox::showPopup ();
bt_->setVisible(true);
//move button to right-down corner
QRect qRect(view()->geometry());
int iXpos=qRect.width()-bt_->width();
int iYpos=qRect.height()-bt_->height();
bt_->move(iXpos,iYpos);
}
void myToolBar::hidePopup ()
{
QComboBox::hidePopup ();
bt_->setVisible(false);
}
To copy to clipboard, switch view to plain text mode
Bookmarks