I have managed to draw some lines on top of the qchartview, if the window isn't resized it is acceptable. However when i resize the line does not scale as shown below.
How can i draw a vertical line so its bound by the current windows rectangle and adjusts when the window is resized?
void ChartView::mousePressEvent(QMouseEvent *event)
{
if (m_isTouching)
return;
if( event->modifiers() & Qt::ShiftModifier )
{
qreal xVal = chart()->mapToValue(event->pos()).x();
qreal yVal = chart()->mapToValue(event->pos()).y();
QGraphicsLineItem* linex = this->scene()->addLine(0+xVal,50,0+xVal,500);
line.push_back(linex);
line.back()->setFlag(QGraphicsItem::ItemIsMovable);
}
if( event->modifiers() & Qt::ControlModifier)
{
for(auto& item : line)
{
if(this->scene() != NULL )
{
this->scene()->removeItem(item);
}
}
}
QChartView::mousePressEvent(event);
}
Related
I have table view with 6 columns in my app. All columns are interactive and a last one also stretches via function QTableView::setStretchLastSection. However, if I try to resize main window from maximum size to minimum(1170x686 for my app), the last column stays stretched. I made a workaround:
void MyTableView::resizeEvent(QResizeEvent* event)
{
QWidget* widget = parentWidget();
while (widget != nullptr && widget->parentWidget() != nullptr)
{
widget = widget->parentWidget();
}
if (widget != nullptr)
{
const int width = widget->width();
const int height = widget->height();
static const QSize s_minimumWindowSize(1170, 686);
QAbstractItemModel* currentModel = model();
if (width <= s_minimumWindowSize.width() && height <= s_minimumWindowSize.height() && currentModel != nullptr)
{
setColumnWidth(currentModel->columnCount() - 1, GetMinimumWidthForColumn());
}
}
QTableView::resizeEvent(event);
}
Is there a better way to fix this issue?
Check the size policy of your table.
Setting both the horizontal and vertical policies to ignored usually fixes this problem for me.
I have a TextField where the user can type. I would like to show a ContextMenu below cursor when the user hit Ctrl+Space key combination.
codeArea.setOnKeyPressed(event -> {
if( event.getCode().equals( KeyCode.SPACE ) && event.isControlDown() ) {
int cursorX = ?;
int cursorY = ?;
cm.show(codeArea, x, y);
} else {
cm.hide();
}
});
How I get the cursor current position? I must give it's (screen) XY coords to the show() function.
I'd like use it for auto completion.
Thanks.
Add a MouseListener and save the last position in a Variable :)
You might wanna use getScreenX() and getScreenY() not sure of the difference at the moment.
codeArea.setOnMouseMoved(new EventHandler<MouseEvent>() {
#Override public void handle(MouseEvent event) {
this.cursorX = event.getX();
this.cursorY = event.getY();
}
});
I am an android developer now trying my hands on unity.I am trying to do a simple drag and drop on a Button with the below code ,but as i drag to the extreme ends the button becomes lost ,I need the drag to take place within the screen width and height and only when i drag on the button.Hence i am new to Unity i need your guidance.I got the below code from unity forum
public class UserInterface : MonoBehaviour {
public Rect playerPositionRect = new Rect(0, 151, 200, 50);
private Vector2 currentDrag = new Vector2();
void Start () {
}
void Update () {
}
void OnGUI(){
GUI.Box (playerPositionRect, "MyButton" );
Vector2 screenMousePosition = new Vector2(Input.mousePosition.x, Screen.height - Input.mousePosition.y);
if (currentDrag.sqrMagnitude != 0 || playerPositionRect.Contains(screenMousePosition)) {
if (Input.GetMouseButtonDown(0)) {
currentDrag = screenMousePosition;
} else if (Input.GetMouseButton(0)) {
playerPositionRect.x += (screenMousePosition.x - currentDrag.x);
playerPositionRect.y += (screenMousePosition.y - currentDrag.y);
currentDrag = screenMousePosition;
} else {
currentDrag.x = 0;
currentDrag.y = 0;
}
}
}
}
After moving the button you need to make sure its still within the screen. The screen edges is Screen.Width, Screen.Height, 0 and 0 and if its outside we just move it back in. When we are at the right and bottom of the screen we also need to adjust for the buttons height and width.
else if (Input.GetMouseButton(0)) {
playerPositionRect.x += (screenMousePosition.x - currentDrag.x);
playerPositionRect.y += (screenMousePosition.y - currentDrag.y);
playerPositionRect.x = Mathf.Clamp(playerPositionRect.x, 0, Screen.width - playerPositionRect.width);
playerPositionRect.y = Mathf.Clamp(playerPositionRect.y, 0, Screen.height - playerPositionRect.height);
currentDrag = screenMousePosition;
}
Clamp just cuts the value if its outside of the range so for example a x value of -5 would be 0 or a value of 10000000 would be Screen.width - playerPositionRect.width.
I want to ask a question about can I resize QGraphicsItem without creating a class that inherits QGraphicsItem. For example, something like this:
void MyScene::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
point = event->scenePos();
if( ArrowCursor )
{
curItem = this->itemAt( point, view->transform() );
if( curItem && !curItem->isSelected() )
{
curItem->update( 10,10, 100, 100 );
}
}
}
curItem->setTransform(QTransform::fromScale(2.0,2.0), true);
Offtopic: This is strange that you are doing this by subclassing QGraphicsScene.
So I have a ON-OFF button that draws a circle. The trouble I am encountering is that the ON OFF states are random depending on how long I press the button. I guess this is due to the draw() function which also loops my button function in time with framerate. What I want is for the button to turn on when pressed once and turn off when pressed again irrespective of how long the button is pressed. Here is the code.
else if (circle4.pressed()) {
println("button 4 is pressed");
if(drawCirclesPrimary){
drawCirclesPrimary = false;
}
else{
drawCirclesPrimary = true;
}
println("drawCirclesPrimary"+drawCirclesPrimary);
}
I would suggest looking at the Buttons tutorial on processing.org. The following code is a subset of what is contained in the tutorial (you will need to review all the code in the tutorial, however). Comments are mine.
void setup() {
// Create instances of your button(s)
}
void draw() {
// Draw buttons, update cursor position, check if buttons have been clicked.
}
// Provides the overRect() method (among others).
class Button
{
// If the cursor is placed within the footprint of the button, return true.
boolean overRect(int x, int y, int width, int height)
{
if (mouseX >= x && mouseX <= x+width && mouseY >= y && mouseY <= y+height) {
return true;
}
else {
return false;
}
}
}
class RectButton extends Button
{
// Create a rectangle button with these size/color attributes.
RectButton(int ix, int iy, int isize, color icolor, color ihighlight)
{
x = ix;
y = iy;
size = isize;
basecolor = icolor;
highlightcolor = ihighlight;
currentcolor = basecolor;
}
// Determines whether the cursor is over the button.
boolean over()
{
if( overRect(x, y, size, size) ) {
over = true;
return true;
}
else {
over = false;
return false;
}
}
// Draws the rectangle button into your sketch.
void display()
{
stroke(255);
fill(currentcolor);
rect(x, y, size, size);
}
}
This thread has some example code for drawing an object only while a key is pressed. That's pretty similar to what you want.
Instead of keyPressed and keyReleased, you can use mouseClicked. which is called once after a mouse button has been pressed and then released. Use a boolean variable to store the on/off state. Inside of mouseClicked, toggle the value of that boolean variable.