I am creating a program that draws graphs; I want to be able to have a button that a user can press to choose the line color and style. I want to be able to visually show what the current selection is.
Currently, I know that I can do that using two seperate widgets, a QPush button, and then a widget I make myself that just draws a line across it using QPen.
I would like to turn these two widgets into a single widget. I want this widget to be a pushable "button" that the user presses and I can get a signal out of to run a routine that sets a new QPen.
Is this functionality built in? Or would I need to create a new widget that re-implements either QPushButton or QActionButton? Or should I just make my widget which listens to mouseclick events on it and create a signal slot from there?
You could use a QLabel, set the style sheet, and use that as your line on the graph. You could use the bounds of a QGroupBox to define x and y axis.
Maybe something like this:
yourQLabel.setStyleSheet("QWidget {background-color: rgb(255, 0, 0); color:white; border: 1px solid black;}"); // red with black border
Then you can set the height, width and position of the QLabel based on values for your graph. Of course this will only work if lines on your graphs are rectangular. If they aren't, then you'll probably have to use something other than QLabel.
Use the setGeometry method of QLabel to set this.
yourQLabel.setGeometry(x, y, width, height);
As far as your button, you can do something similar with that if you want. A QPushButton also has the setGeometry and setStyleSheet methods.
If you want to tell your button to do something on click, assuming you have a QPushButton object called myButton, add this to your header file (.h):
class YourClass
{
Q_OBJECT
//...
private slots:
void on_myButton_clicked();
}
Then in your source file (.cpp):
void YourClass::on_myButton_clicked()
{
// stuff your button does on click
}
Related
I have a QWidget which should take place over the central widget of my application which is a VideoWidget for playing video. The problem is that I can't set background of the QWidget to be transparent.
Use a StyleSheet (you can do that in code with setStylesheet or in the ui file : RMB on the widget you want "Change stylesheet" or even in the properties tab)
for instance
"background-color: rgba(255, 255, 0, 50);"
gives a yellow transparant look:(example of a button used on top of another button)
With stylesheets you can control the look of a complete program or just the specific widgets you want. More info in the style sheet reference
You could make the full Widget invisible:
QWidget::setHidden(true);
How can I modify an existing stylesheet?
For example: if I want to create buttons, which when pressed each modify a single aspect of the stylesheet. One button can insert a margin-left attribute of 10. Another button can make the background colors blue. Lastly, another button can round the corners. The trick here though, is that I dont want to store all the variables and rebuild the style sheet on each button press. I would like to have a simple this->setStyleSheet(this->getStylesheet()+"margin-left: 10px:") for example.
Is there any way to do this?
Here is the code in main.cpp
QWidget wdg;
QHBoxLayout hlay;
wdg.setStyleSheet("border:2px solid rgb(74, 74, 74);");
QPushButton btn;
btn.setStyleSheet("border-radius:5px;");
btn.setText("Hello");
QPushButton btn2;
btn2.setStyleSheet("background-color: rgb(190, 190, 190);");
btn2.setText("Hello");
hlay.addWidget(&btn);
hlay.addWidget(&btn2);
qDebug()<<btn.styleSheet();
wdg.setLayout(&hlay);
wdg.show();
setting and getting style sheet works with QString and so you can use + operator.
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);
I've a class that extends QWidget and contains a QLabel (lblBackground). I've overriden paintEvent function too.
I want to draw something on top of lblBackground however paintEvent method is called before the QLabel is drawn. Thus my custom drawings are overwritten.
Is there a way to change drawing order?
Painting the children on top of their parent is the common thing to do. That being said you could try one of the following options:
extend QLabel itself to paint whatever you want
try to set the Qt::WA_TranslucentBackground flag on the QLabel and having an alpha channel, so that the underlying parent (QWidget) would shine through
if you are only using the QLabel to paint some background, maybe you can get rid of it and paint the desired background first thing in the QWidget's paintEvent()?
If you want to use label as a background then just create your custom widget as a child of your label. May be split some window frame related tasks if any (to be implemented as a parent of the label) and drawing/controls/etc (to be child of the label).
Is there a way to change the background color of a QPushButton which is pressed by modifying its QPalette object? I realize it can be done with style sheets but would like a way to do it by modifying the QPalette. Something similar to how one changes the background color of the button when not pressed:
QPushButton myButton;
QPalette p(myButton.palette());
p.setColor(QPalette::Button, QColor("#ffffff"));
myButton.setPalette(p);
Simply add a stylesheet to the qbushbutton itself or to his parent qwidget:
qwidget.setStyleSheet("QPushButton:checked { background-color: red; }")
This will set the background color to red when the QPushButton is checked.
Connect to QPushButton's pressed() signal and update the palette with your desired color and then connect to released() signal and switch back the color to the old color. That should do it.
You may want to run update() after settings the colors or in extreme cases repaint().
Connect to the button's pressed signal and set the palette in the connected slot. You may have to call repaint() to force an immediate update.
you can set stylesheet for button when it is pressed.. here is the Example tells how to set style sheet for button.. but in your case u need to have two different style sheets, one is button pressed and similarly when it is released you should reset with some other stylesheet.