0
votes

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?

1

1 Answers

0
votes

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
}