I have a QLineEdit, on which I have set a QRegExpValidator, that allows the user to input only one whitespace between words.
Now I want that whenever the user tries to enter more than one whitespaces, the tooltip of the QLineEdit should show up, but I'm not getting any method to implement it.
Thanx :)
It seems there is no direct method to perform what you want. One way to do above is to handle QLineEdit's textChanged() signal. Then you can check that string against your regular expression using QRegExp::exactMatch() function and if it don't match then show tooltip.
Connect the signal..
...
connect(ui->lineEdit,SIGNAL(textChanged(QString)),this,SLOT(onTextChanged(QString)));
...
Here your slot goes..
void MainWindow::onTextChanged(QString text)
{
QRegExp regExp;
regExp.setPattern("[^0-9]*"); // For example I have taken simpler regex..
if(regExp.exactMatch(text))
{
m_correctText = text; // Correct text so far..
QToolTip::hideText();
}
else
{
QPoint point = QPoint(geometry().left() + ui->lineEdit->geometry().left(),
geometry().top() + ui->lineEdit->geometry().bottom());
ui->lineEdit->setText(m_correctText); // Reset previous text..
QToolTip::showText(point,"Cannot enter number..");
}
}
I don't remember explicit API for showing a tooltip. I'm afraid you'll have to go with popping up a custom tool window (i.e. a parent-less QWidget) to achieve the desired result.
If you want to style your own popup window like a standard tooltip, QStyle should have something for that. If in doubt, read into the Qt source code where it renders the tooltip. That'll tell you which style elements to use.
Related
I'm developing a RCP application, where I need to assign shortcut (Alt+[key]) to SWT Button with image.
I'm able to assign shortcut to Button with text using "&" character,
fox ex: button.setText("&Select All"); then Alt+S will act as shortcut fot that button.
I have 2 questions regarding this:
How to assign Alt+[key] shortcut to SWT button with images (no text), without using key listener?
How to assign Alt+[key] shortcut to SWT button with text, but no shortcut key letter in that text, again without using key listener.
for ex: "UnFix" is the text on button and shortcut key should be Alt+Q.
I hope there should be a way to do this in SWT.
You can select a mnemonic by placing an ampersand before the letter that should serve as the mnemonic letter (like "&Select All").
If the control does not have a text or the desired letter does not occur in that text, you will ned to use key event listener. There is no way around that.
Some applications work around the 'missing letter' in that they place the mnemonic letter in brackes like this: "UnFix (&Q)". Though this technically work, I find this an esthetically rather unfortunate choice.
Using an unobvious mnemonic letter has also usability issues: how would a user ever know or memorize that Alt+Q means 'UnFix'?
After trying lots of work arounds, I'm finally able to assign shortcut key to SWT button with image.
SWT Button.class make use of getText() method to find mnemonic, so overring getText() method of Button.class and returing mnemonic key will serve the purpose. checkSubclass() method should also be overridden to do nothing otherwise swt's Subclassing not allowed error will occure.
Button newButton = new Button(parent, SWT.PUSH){
#Override
public String getText() {
return "&N";
}
#Override
protected void checkSubclass() {
// Do Nothing to avoid Subclassing Not Allowed error.
}
};
newButton.setImage(newButtonImage);
I above example newButton has the shortcut key Alt+N.
I am creating an winAPI application in c++ I have a photo in preview pane and I want to create two buttons NEXT and PREVIOUS on clicking them I will go to the next page .
Could you please give me the idea how to do that in c++ ??
Do I need to use QT libraray or it can be done using the in built function of WinAPI like -
HWND hwndButton1 = CreateWindow(L"BUTTON",L"NEXT",WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON,550,800,100,30,m_hwndPreview,(HMENU)buttonid1,(HINSTANCE)GetWindowLong(m_hwndPreview, -6),NULL);
HWND hwndButton2 = CreateWindow(L"BUTTON",L"PREVIOUS",WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON,650,800,100,30,m_hwndPreview,(HMENU)buttonid2,(HINSTANCE)GetWindowLong(m_hwndPreview, -6),NULL);
and then using WM_COMMAND for both the button clicks.
Am I going right?
I just want my API application work like a .pdf extension file...as in PDF files we have up and down arrow and on clicking upon them we can go to the next page..In winAPIc++ I couldn't find any such arrow function.. please tell me if there is any such arrow up/down function present to go to next page (because I am very less interested in creating NEXT and PREVIOUS button using createwindow function.. It looks odd).
You have not mentioned what tools you are using, so we don't know if you have a resouce editor. You should research that in a forum appropriate for the tools. If you think writing one line of code to create a button is "very complicated" then you need a better tool.
If you do not want the buttons to appear on top of the picture then you need another place to put them. One common possibility is a toolbar. It is a strip for buttons along the top or bottom of the main window:
http://msdn.microsoft.com/en-us/library/windows/desktop/bb760435(v=vs.85).aspx
With a resource editor you can draw an arrow on the button. Without a resource editor you can set the button text to a unicode arrow:
SetWindowText(hwndButton1, L"\x25bc"); // down arrow, use 25b2 for up arrow
Most buttons (and other controls) are created using a resource editor, placing the controls on a dialog template or a toolbar resource. If you do that Windows will create the buttons when you create the dialog or toolbar. This method is much preferred because Windows will adjust the size of the buttons as required for the screen settings in use.
If you can't do that you must use CreateWindow as you are doing.
Finally it is done.. I have created the buttons neither using Qt or nor using any createWindowEx..The best and easy approach to follow is resource editor ...just put some button on dialog and use IDD_MAINDIALOG (in my case)
m_hwndPreview = CreateDialogParam( g_hInst,MAKEINTRESOURCE(IDD_MAINDIALOG), m_hwndParent,(DLGPROC)DialogProc, (LPARAM)this);
and then
BOOL CALLBACK AMEPreviewHandler::DialogProc(HWND m_hwndPreview, UINT Umsg, WPARAM wParam, LPARAM lParam)
{
switch(Umsg) // handle these messages
{ .........
}
....
}
and thats done. Very easy task.
I want to show only icons in my QListWidget. I set text to empty string. When I select an icon I see an empty selected square on the text place. See the screenshot:
How can I get rid of this empty space?!
use NULL instead
ui->listWidget->addItem(new QListWidgetItem(QIcon(":/res/icon"),NULL));
How do you add an icon in your QListWidget? This should work fine (I am loading the icon from the resource file) :
ui->listWidget->addItem(new QListWidgetItem(QIcon(":/res/icon"), ""));
EDIT
From the screenshot I see that your problem is that there is some white space below the icon corresponding to the empty string. You could hack this behavior by setting a very small size to the font of the list widget item.
QListWidgetItem *newItem = new QListWidgetItem;
QFont f;
f.setPointSize(1); // It cannot be 0
newItem->setText("");
newItem->setIcon(QIcon(":/res/icon"));
newItem->setFont(f);
ui->listWidget->addItem(newItem);
This will do the trick. However you could also use the setItemWidget function and use your custom designed widget, or use a QListView and a delegate.
My solution was to call setSizeHint() on the item with the size of the icon. I added a little padding because the selection box was cut off without it.
QListWidgetItem * pItem = new QListWidgetItem(icon, "");
pItem->setSizeHint(iconSize + QSize(4,4));
listWidget->addItem(pItem);
An alternative solution where you do want to store text (as an identifier) but not show it, is not to set ANY text for the QListWidgetItems in the creator but instead store the text details in the data part.
Specifically you want to use this QListWidgetItem(QListWidget *parent = nullptr, int type = Type) constructor that can be used without any arguments. Then you can assign an icon, but no text afterwards, before inserting it into the QListWidget.
If you are putting the text in afterwards anyhow you'd be using QListWidgetItem::setText(const QString &text) and you just need to change that to QListWidgetItem::setData(int role, const QVariant &value) which in practice would be QListWidgetItem::setData(Qt::UserRole, const QString &text) and rely on a QString being convertible to a QVariant. Unfortunately there are some shortcomings going the other way:
To retrieve the value you need to explicitly convert the QVariant back to a QString - by using QListWidgetItem::data(Qt::UserRole)->toString()
To search for a particular match Qt does not provide a find means to identify the index(es) of the members of a QListWidget that match a particular role though it does for the original text - unlike, say a QComboBox which has findXXXX methods for both.
The project I code for had exactly this issue (we had unwanted text at the bottom which was set to a font size of 1 but it still showed up) which I eventually fixed like this and it works:
I want to show the user a warning QMessageBox with a link inside. This is relatively easy, I just need to make sure I set the RichText text format on the message box and the QMessageBox setup does the rest. However, I would also like to close the message box (as in some-sort-of-call-to done()) if the user clicks on the link - the semantic being that the user acknowledged the message and made a decision.
The problem: QMessageBox hides the linkActivated signal coming from its inner QLabel (which is used to store the text).
I thought I could extend the QMessageBox class and do this very ugly hack in the constructor:
QLabel *lbl = findChild<QLabel*>(QString("qt_msgbox_label"));
assert(lbl != NULL);
connect(lbl, SIGNAL(linkActivated(const QString&)), this, SLOT(handle_link_activation(const QString&)));
but although the label found with findChild is not null, and the "qt_msgbox_label" is definitely correct (c/p'ed from the source), and there is no "no such signal/slot" message, my slot never gets called when I click the link.
I'd like to avoid writing my own QDialog which would mimic the QMessageBox behavior. Does anyone have any idea on how I can catch that signal?
Try defining your own link "protocol" i.e. msgboxurl://yoururl.is.here and install url handler for it
QDesktopServices::setUrlHandler("msgboxurl", urlHandlerObj, "slotName");
urlHandlerObj may be object that created message box. In slot you can just hide your message box and take url part after // and open it with QDesktopServices::openUrl but remember that you have to prepend then http/https prefix (on some platforms url without "scheme" is not handled properly). Slot handling url must have same parameters as QDesktopServices::openUrl static method
Is there any code or custom options available to achieve the following :
1> When an error occurs in a text box, the validation shows the error. Forces the user to remove the error and only then proceed to complete remaining text inputs. KEEPS the mouse focus on the Text Box.
I have used built in mx:Validator tags, but it does not coerce the user to remove the error. Instead, user can easily go ahead without rectifying the error.
2> Can the error message which generally appears as a tooltip when mouse focus moves over the text input with the error, REMAIN until the user removes error and not just be displayed on mouse hover action?
You can customize your ToolTips to show your Error. Check this link to customize your tooltip, to show your error in ToolTips
For #2, check out http://aralbalkan.com/1125.
Unfortunately, it is a lot of hassle if you have multiple/large forms. It is unfortunate flex doesn't provide more styling options for the error tooltip.
#1 seems to be a bad UI design. While you may not allow them to submit a form unless they enter valid information, they should be able to navigate around the form freely and fill in the information as they choose. Just my opinion.
A solution to question 1) is as follows;
Use the Validator.validateAll static method to check that all form items are valid before allowing the form to be submitted. The following snippet is taken from a good flex example which shows this
private function resetForm() :void
{
btnLogin.enabled = false;
}
private function validateUs() :void
{
btnLogin.enabled = (Validator.validateAll([val1,val2]).length == 0);
}
The complete example is here
http://idletogether.com/easy-form-validation-and-submit-button-enable-disable-in-flex-3/