Currently I have a QComboBox who's color I want to change when the user makes a selection. To do this I have function called on the activated signal which calls setStyleSheet
void comboBoxActivated(int i){
myComboBox -> setStyleSheet("border-width:2px; border-style:solid; border-color:red;");
}
This however causes the combo box to be stuck open on my screen and the only way to get rid of it is close the application. Is there some issue with changing the style using this function? For reference I am running Ubuntu and QT 4.8
This code in windows works perfect:
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->comboBox->addItem("item1");
ui->comboBox->addItem("item2");
ui->comboBox->addItem("itme3");
connect(ui->comboBox, SIGNAL(activated(int)),
this, SLOT(comboBoxActivated(int)));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::comboBoxActivated(int i){
ui->comboBox -> setStyleSheet("border-width:2px; border-style:solid; border-color:red;");
}
For some reason if I force the box to open and close the change will apply without issue
void comboBoxActivated(int i){
myComboBox -> setStyleSheet("border-width:2px; border-style:solid; border-color:red;");
myComboBox -> showPopup();
myComboBox -> hidePopup();
}
Related
I have added a QTabWidget which is checkable. I want to hide all tabs (panes only) when the TabBar is unchecked and vice versa. Is there any way to make only pane invisible and tab bar will not disappear?
I add the image reference related to what I want in output:
Initially both tab pane is minimize and when i click on the tab it will maximize:
after clicking pane is maximize and again i click it will minimize and vice versa:
Instead of using QTabWidget. You can use a QTabBar and implement the functionality you desire hiding the corresponding widget.
here is some sample code of a new widget application example within qt creator
#include "MainWindow.h"
#include "ui_MainWindow.h"
#include <QTabBar>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
m_tabbar = new QTabBar(this->centralWidget());
m_tabbar->addTab("Hello");
m_tabbar->addTab("World");
m_tabbar->setShape(QTabBar::RoundedWest);
m_tabbar->setGeometry(0,0,this->height(), 200);
connect(m_tabbar, SIGNAL(tabBarClicked(int)), this, SLOT(changedTab(int)));
}
MainWindow::~MainWindow()
{
delete ui;
}
void
MainWindow::changedTab(int idx)
{
m_tabbar->setCurrentIndex(idx);
ui->stackedWidget->setCurrentIndex(idx);
}
Pretty much this window opens up and asks for a bandname. I got it so the characters on the line edit widget get stored in a variable. Problem is I have another file called main window.cpp and I want that variable to to be stored on the list widget on that window. Now I know how to display things on the list widget but I can't figure out a way to get the text after the user finished typing. The bandname var in the main window.cpp file just takes an empty string and I know why but is there any way to trigger the get call after the user has finished typing. Do I have to restrict something in the class like the get function. I've experimented a lot and saw callbacks but I could just use signals and slots. Everything Ive tried just returns an empty string but I need the text after the user has finished typing what he wants. Here is the dialog window named add button
#include "addbutton.h"
#include "ui_addbutton.h"
AddButton::AddButton(QWidget *parent) :
QDialog(parent),
ui(new Ui::AddButton)
{
ui->setupUi(this);
connect(ui->cancel,SIGNAL(released()),this,SLOT(close()));
//Get Text when user presses enter
connect(ui->lineEdit, SIGNAL(editingFinished()),this,SLOT(setBandName()));
}
void AddButton::setBandName(){
bandname = ui->lineEdit->text();
}
void AddButton::updateState(){
pbandname = bandname;
}
QString AddButton::getBandName(){
return bandname;
}
AddButton::~AddButton()
{
delete ui;
}
Here is the main window.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "addbutton.h"
#include "bandinfo.h"
#include "QDebug"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
mediaplayer = new Player;
connect(ui->pushbutton_addBand,SIGNAL(pressed()),this,SLOT(addBand()));
}
void MainWindow::addBand(){
BandInfo band;
AddButton *addband_window = new AddButton;
QString bandname;
addband_window->show();
bandname = addband_window->pbandname;
qDebug() << bandname;
}
MainWindow::~MainWindow()
{
delete ui;
}
I'm not sure I understand your post. Are you saying you want the main window to be notified/updated when the user finishes editing in the "AddButton" class?
If I've got that right it seems pretty straightforward:
Add a signal to the AddButton class. Call it something like "bandNameChanged".
Make the signal pass a string as its argument
Emit the signal from within AddButton::setBandName and pass the string name.
Have the main window connect a slot to the "bandNameChanged" signal when it creates the AddButton.
In the slot, update your list widget
I am new to Qt. I took an example from here http://doc.qt.io/qt-5/qtmultimediawidgets-player-example.html.
Now I want to integrate the player in the main window. I created a Qt Widgets application project, I thought, that I would just have to edit the main window code:
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
Player* player;
MainWindow::setCentralWidget(player);
}
But it doesn't work and I get the following error:
Starting /home/***/Documents/build-player-Desktop-Debug/player...
The program has unexpectedly finished.
/home/***/Documents/build-player-Desktop-Debug/player crashed
How can I integrate a custom widget which is written in code, without ui in a main window? Thank you in advance.
In your own MainWindow class you can add a widget to the layout of that MainWindow:
MyMainWindow::MyMainWindow(QWidget *parent) :
...
{
this->ui->setupUi(this);
QLabel *myLabel = new QLabel();
this->layout()->addWidget(myLabel);
}
Well, player can't be placed on the window if it is not initialized.
Write something like that :
Player *player = new Player();
I usually add a QWidget (or whatever widget type I'm extending) to my .ui-file in the designer and then promote it to the actual derived type. See the Qt docs for more info on promoting widgets. This means that I can set the base widget's properties and design the window as usual but still get an instance of my special class when the UI is instantiated.
MainWindow:MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
SomeStupidWidget *ssw = new SomeStupidWidget(this); /* important! don't forget about passing "this" as argument, otherwise this could cause a memory leak(Qt handles object's lifetime by means of it's "owner" mechanism)*/
layout()->addWidget(ssw);
}
I'm trying to perform action, when user clicks on button.
My code is:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtUiTools/QUiLoader>
#include <QFile>
#include <QMessageBox>
#include <QFileDialog>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
createActions();
}
void MainWindow::test()
{
//QMessageBox::information(this, "Welcome", "Select first image.");
//QFileDialog::getOpenFileName(this, QT_TR_NOOP("Open Image"), "D:\\", QT_TR_NOOP("Image Files (*.png *.jpg *.bmp)"));
resize(100,500);
}
void MainWindow::createActions()
{
QWidget *centralWidget = this->centralWidget();
QPushButton *buttonBack = centralWidget->findChild<QPushButton *>("pushButton");
QObject::connect(buttonBack,SIGNAL(clicked()), this, SLOT(test()));
QAction *open = this->findChild<QAction *>("actionOpen");
//QMessageBox::information(this, "Welcome", open->text());
connect(open, SIGNAL(triggered()), this, SLOT(test()));
}
Function void MainWindow::test() is defined as SLOT in header file and I'm sure, that QPushButton *buttonBack isn't null. What I'm doing wrong?
In my code I tried also to perform action through QAction, but in this case, function is performed, when I close window.
It looks like you're setting all your actions in the destructor.
Think about what this is doing:
UI starts up all happy with the setupUi() call from the constructor
Throughout the lifetime of the UI, there are no buttons assigned to any slots. This means the signal from your button will never get to the slot test().
Upon exit of the UI, the button is connected to the slot.
If you want this to happen, that's cool, but if you want the button to connect to the slot while the UI is running, move your createActions() function into the constructor.
Good luck!
code
News::News(QWidget *parent) :
QDialog(parent),
ui(new Ui::News)
{
ui->setupUi(this);
ui->webView->page()->setLinkDelegationPolicy(QWebPage::DelegateAllLinks);
connect(ui->webView,SIGNAL(linkClicked(QUrl)),this,SLOT(openUrl(QUrl)));
}
void News::openUrl(QUrl url){
if(the new window)
QDesktopServices::openUrl(url);
}else{
ui->webView->load(url);
}
}
How do I judge instead of a new window link function openUrl?
How I'm going to write this code、?
thanks!!!
You need to inherit your own class from QWebView and reimplement the createWindow() method. http://doc.qt.digia.com/qt/qwebview.html#createWindow