Qt4: Decorating a QLineEdit (painting around it) - c++

I am trying to "decorate" a QLineEdit, or to be more accurate, to paint my own custom frame around it, to get the following result:
I tried to use Qt Style Sheets (CSS), but this will only enable trivial frame decorating (changing width/color/size etc...), nothing fancy like the above.
I also tried inheriting from QLineEdit and overriding its void QLineEdit::paintEvent(QPaintEvent* e), but then I realized that reimplementing it means I will lose the QLineEdits "editness" (sorry for butchering the language here) - the textbox, the cursor, and the ability to insert text.
How can I achieve the above text box?
Is this a combination of QLabel perfectly located behind a QLineEdit?

Try to use composition. Create your own Widget inherited it from QWidget, paint what you want in QWidget::paintEvent and place QLineEdit above it. Probably you'll have to center it and use css for QLineEdit to make it look smooth.
class MyWidget: public QWidget
{
explicit MyWidget(QWidget* parent = 0):
QWidget(parent),
line_edit(new QLineEdit(this))
{
// place line_edit in center of QWidget
}
private:
QLineEdit* line_edit;
}
Or you can override void QLineEdit::paintEvent(QPaintEvent* e) like this
void QLineEdit::paintEvent(QPaintEvent* e)
{
//paint your border
QLineEdit::paintEvent(e);
}
And you won't lose the QLineEdits "editness".

Related

How do I make a QWidget automatically scale vertically to it's contents?

I've made a QT Designer Form called DropDownMenu. Essentially it is just a QWidget with a QVBoxLayout inside of it.
DropDownMenu has a function that pragmatically adds buttons to it.
QPushButton* DropDownMenu::AddButton(
const QString& text)
{
QPushButton* new_button = new QPushButton(text, this);
m_ui->LayoutManager->addWidget(new_button);
return new_button;
}
I then add a QWidget to my MainWindow inside of QT Designer & promote this widget to DropDownMenu. Then I add buttons to this new QWidget using the AddButton function.
The end result looks like this...
I want to make it so the containers scale according to how many buttons or widgets are placed inside of the layout, but they seem to just get squeezed together so that they fit the parents default size.
How can I make it so the parent scales to the size of all it's children?

How to create QToolBar in QWidget?

I am trying to add a QToolBar in a QWidget. But I want its functionality to work as if it was a QMainWindow.
Apparently I can not create QToolBar in a QWidget, and using setAllowedAreas does not work with QWidget : it only works with QMainWindow. Also, my QWidget is in a QMainWindow.
How can I create a QToolBar for my widget?
The allowedAreas property only works when the toolbar is the child of a QMainWindow. You can add the toolbar to a layout, but it won't be movable by the user. You can still relocate it programmatically, however.
To add it to a layout for a fictional class inheriting QWidget:
void SomeWidget::setupWidgetUi()
{
toolLayout = new QBoxLayout(QBoxLayout::TopToBottom, this);
//set margins to zero so the toolbar touches the widget's edges
toolLayout->setContentsMargins(0, 0, 0, 0);
toolbar = new QToolBar;
toolLayout->addWidget(toolbar);
//use a different layout for the contents so it has normal margins
contentsLayout = new ...
toolLayout->addLayout(contentsLayout);
//more initialization here
}
Changing the toolbar's orientation requires the additional step of calling setDirection on the toolbarLayout, e.g.:
toolbar->setOrientation(Qt::Vertical);
toolbarLayout->setDirection(QBoxLayout::LeftToRight);
//the toolbar is now on the left side of the widget, oriented vertically
QToolBar is a widget. That's why, you can add a QToolBar to any other widget by calling addWidget for layout or by setting the QToolBar parent to your widget.
As you can see in documentation of QToolBar setAllowedAreas method:
This property holds areas where the toolbar may be placed.
The default is Qt::AllToolBarAreas.
This property only makes sense if the toolbar is in a QMainWindow.
That's why it is impossible to use setAllowedAreas if toolbar is not in QMainWindow.
As far as I know, the only way to properly use the toolbar is with the QMainWindow.
If you want to use the full functionality of the toolbar, create a mainwindow with the window flag Widget. This way you can add it inside some other widget without having it displayed as a new window:
class MyWidget : QMainWindow
{
public:
MyWidget(QWidget *parent);
//...
void addToolbar(QToolBar *toolbar);
private:
QMainWindow *subMW;
}
MyWidget::MyWidget(QWidget *parent)
QMainWindow(parent)
{
subMW = new QMainWindow(this, Qt::Widget);//this is the important part. You will have a mainwindow inside your mainwindow
setCentralWidget(QWidget *parent);
}
void MyWidget::addToolbar(QToolBar *toolbar)
{
subMW->addToolBar(toolbar);
}

Implementing mousePressEvent and setDragMode in Qt

I am doing a project in which I should implement mousePressEvent and setDragMode on QGraphicsScene. I am able to implement individually but I should implement both at the same time. I included both into the code but only mousePressEvent is working and not able to drag the scene . Could you please anyone tell me that how to implement both.Please find the attached code. Thankyou.
MyGraphicsView.cpp
MyGraphicsView::MyGraphicsView(QWidget* parent) : QGraphicsView(parent) {
Scene = new QGraphicsScene(this);
setScene(Scene);
setDragMode(ScrollHandDrag);
}
void MyGraphicsView::mousePressEvent(QMouseEvent *eve)
{
if (eve->button() == Qt::LeftButton) {
// handle Left mouse button here
QPointF pt = mapToScene(eve->pos());
}
}
MyGraphicsView.h
class MyGraphicsView : public QGraphicsView
{
Q_OBJECT
protected:
virtual void mousePressEvent(QMouseEvent *eve);
}
From QGraphicsView Qt docs on DragMode property:
(...) The default value, NoDrag, does nothing.
This behavior only affects mouse clicks that are not handled by any item. You can define a custom behavior by creating a subclass of QGraphicsView and reimplementing mouseMoveEvent().
This is probably why you can't make a DragMode to get to work when you implement a mousePressEvent: The event is processed, so the Qt framework thinks the "job is done", and no need to trigger a drag.
The doc suggest to re-implement the mouseMoveEvent. It still provides a QMouseEvent, so you can get the coordinates, and the information that what mouse button is currently pressed. From that on you only have to implement a state machine for Idle; StartDrag; Drag; FinishDrag
according to what buttons are pressed during a move, and implement your desired functionality in whichever states you need them.

Viewing entire QGraphicsScene

I'm trying to write a map editor in Qt, using QGraphicsView and QGraphicsScene for both the map and tile sheets.
The problem I'm having right now is with making a good widget for importing tiles. For this, I'm using a QTabWidget (for different tile sheets), and TileWidget as the widget for each tab, which contains the QGraphicsScene and QGraphicsView.
It's working to a rough degree, but not all the tiles (or TileObjects, which are implementations of QGraphicsItem) are visible. I'm even calling view->ensureVisible(scene->sceneRect()), but still not all of the QGraphicsScene is not visible, even with scroll bars.
I understand this is due to limiting the maximum size of my QTabWidget, but that is necessary.
This happens mainly when I import a larger tile sheet.
I have a TileWidget as the QWidget for the QTabWidget, which has both the QGraphicsScene and the QGraphicsView.
TileWidget::TileWidget(QWidget *parent)
: QWidget(parent)
{
scene = new QGraphicsScene;
view = new TileView(scene, this);
connect(view, SIGNAL(newBrushSelected(TileObject *b)), this, SLOT(selectNewBrush(TileObject *b)));
}
TileView is simply a QGraphicsView re-implemented to handle mouse release events.
To add tiles, I simply call scene->addItem().
I have no other code for TileView. When I use
void TileWidget::showEvent(QShowEvent *event)
{
view->fitInView(scene->itemsBoundingRect(), Qt::KeepAspectRatio);
}
I get something like this.
It's okay for smaller tile sheets, but not for larger ones. What should I add to keep the size of the tiles normal, and navigate TileView using scroll bars?
Nevermind, figured it out. Just me being stupid.
You need something like:
p_myGraphicsView->fitInView(
myGraphicsView->scene()->itemsBoundingRect(),
Qt::KeepAspectRatio);

Setting a QDialog to be an alien

In a standalone GUI application where I don't have a windowmanager nor a composite manager I want to display a QDialog to the user to ask for values.
The dialog is quite big, so I want to make it translucent so that the user can look through it to see what happens in the application while the dialog is shown.
The problem is that for translucency of X native windows, there needs to be a composite manager. Qt internal widgets can be painted translucent because they don't correspond to native X windows (aliens) and are completely only known to Qt.
Is there a way to make the background of a QDialog translucent without having a composite manager running? Perhaps making it a normal child widget/alien of the application's main window? Is there a better alternative to this?
I don't know of any way of turning a QDialog into a normal child widget. Looking at the Qt for X11 code, I can't figure out a way of not setting the Qt::WindowFlags passed to the QWidget (parent) constructor so that it would be a plain widget and not a window of its own (but I could be wrong, didn't spend a lot of time on that).
A simple alternative is to use a plain QWidget as your container, instead of a QDialog. Here's a sample "PopupWidget" that paints a half-transparent-red background.
#include <QtGui>
class PopupWidget: public QWidget
{
Q_OBJECT
public:
PopupWidget(QWidget *parent): QWidget(parent)
{
QVBoxLayout *vl = new QVBoxLayout;
QPushButton *pb = new QPushButton("on top!", this);
vl->addWidget(pb);
connect(pb, SIGNAL(clicked()), this, SLOT(hide()));
}
public slots:
void popup() {
setGeometry(0, 0, parentWidget()->width(), parentWidget()->height());
raise();
show();
}
protected:
void paintEvent(QPaintEvent *)
{
QPainter p(this);
QBrush b(QColor(255,0,0,128));
p.fillRect(0, 0, width(), height(), b);
}
};
To show it, call it's popup() slot which will raise it to the top of the widget stack, make it as large as its parent, and show it. This will mask all the widgets behind it (you can't interact with them with the mouse). It hides itself when you click on that button.
Caveats:
this doesn't prevent the user from using Tab to reach the widgets underneath. This could be fixed by toggling the enabled property on your "normal" widget container for example. (But don't disable the PopupWidget's parent: that would disable the popup widget itself.)
this doesn't allow for a blocking call like QDialog::exec
the widgets in that popup won't be transparent, you'd have to create custom transparent-background versions for all the widget types you need AFAIK.
But that's probably less of a hassel than integarting a compositing manager in your environment.